Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Choosing only numbers from the output
_
Hi

I have an output which looks like this:

Tag       : Result
146603    :
147020    :  
147155    : NONE 
147160    :
147232    :
147243    :  
147254    : none 
147318    :
147341    : NONE
147430    : N/A 
147708    :
147710    : 0000 
147729    :  
147768    :  
147851    :
147921    :
147949    : 0000 
147981    :  
147983    :  
147991    :
148007    : 0000 
148080    :  
148105:
148200    :
148312    : 38160 
148329    : 44139 
148444    :NONE 
148514    :  
148573    : 0000 
148697    :
148759    :  
148919    :
148920    :
148921    :
148923    :
148924    :
148933    :
148934    :
148935    :
148936    :
148937    :
148938    :
148939    :
148951    :
148996    :

Now i want to gather all the "Tags" for which the "Result" is a number (but 
shouldnt include the tag if the result is 0). The corresponding Result shoul
d go to another array. 

Considering the above example my array1 should contain 148312 and 148329 and
 array2 should contain 38160 and 44139 

How do i do this?

Thanks in advance

Regards
Sudhindra















Report this thread to moderator Post Follow-up to this message
Old Post
Sudhindra K S
08-04-04 01:58 PM


Re: Choosing only numbers from the output
Sudhindra K S wrote:
> I have an output which looks like this:

Do you possibly mean input? ;-)

> Tag       : Result
> 148200    :
> 148312    : 38160
> 148329    : 44139
> 148444    : NONE
> 148514    :
> 148573    : 0000
> 148697    :

<similar lines snipped>

> Now i want to gather all the "Tags" for which the "Result" is a
> number (but shouldnt include the tag if the result is 0). The
> corresponding Result should go to another array.
>
> Considering the above example my array1 should contain 148312 and
> 148329 and array2 should contain 38160 and 44139
>
> How do i do this?

while (<DATA> ) {
my ($tag, $result) = split /[ :]+/;
if ($result =~ /^\d+$/ and $result > 0) {
..

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl




Report this thread to moderator Post Follow-up to this message
Old Post
Gunnar Hjalmarsson
08-04-04 01:58 PM


Re: Choosing only numbers from the output
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

sudhindra k s wrote:
|
| Hi

Hello,

| I have an output which looks like this:
|
| Tag       : Result
| 146603    :
| 147020    :
| 147155    : NONE
| 147160    :
| 147232    :
| 147243    :
| 147254    : none
| 147318    :
| 147341    : NONE
| 147430    : N/A
| 147708    :
| 147710    : 0000
| 147729    :
| 147768    :
| 147851    :
| 147921    :
| 147949    : 0000
| 147981    :
| 147983    :
| 147991    :
| 148007    : 0000
| 148080    :
| 148105    :
| 148200    :
| 148312    : 38160
| 148329    : 44139
| 148444    : NONE
| 148514    :
| 148573    : 0000
| 148697    :
| 148759    :
| 148919    :
| 148920    :
| 148921    :
| 148923    :
| 148924    :
| 148933    :
| 148934    :
| 148935    :
| 148936    :
| 148937    :
| 148938    :
| 148939    :
| 148951    :
| 148996    :
|
| Now i want to gather all the "Tags" for which the "Result" is a number (bu
t shouldnt include the tag if the result is 0). The corresponding Result sho
uld go to another array.
|
| Considering the above example my array1 should contain 148312 and 148329 a
nd array2 should contain 38160 and 44139
|
| How do i do this?

I would use an array of arrays or an array of hashes instead of two
separate arrays.


my @tag_and_results;
while ( <FILE> ) {
~    next unless /^(\d+)\D+([1-9]\d*)/;
~    push @tag_and_results, [ $1, $2 ];
~    }




John
- --
use Perl;
program
fulfillment
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

 iD8DBQFBEMgW5dGfmBZ1ssARAmiKAJ43Qy6ysZoU
R/GURjurXf4FMlVNCACfSn/J
VQZ4Wxrgr9T6J7zbLXJDOPM=
=C2SM
-----END PGP SIGNATURE-----

Report this thread to moderator Post Follow-up to this message
Old Post
John W. Krahn
08-04-04 01:58 PM


RE: Choosing only numbers from the output
One way would be to split each line using : as the delimiter.. 

I am sure there are others, but I think split would be the easiest... IMHO



From: sudhindra k s
Sent: Wed 8/4/2004 5:48 AM
To: beginners@perl.org
Subject: Choosing only numbers from the output


 
Hi

I have an output which looks like this:

Tag       : Result
146603    :
147020    :  
147155    : NONE 
147160    :
147232    :
147243    :  
147254    : none 
147318    :
147341    : NONE 
147430    : N/A 
147708    :
147710    : 0000 
147729    :  
147768    :  
147851    :
147921    :
147949    : 0000 
147981    :  
147983    :  
147991    :
148007    : 0000 
148080    :  
148105    :
148200    :
148312    : 38160 
148329    : 44139 
148444    : NONE 
148514    :  
148573    : 0000 
148697    :
148759    :  
148919    :
148920    :
148921    :
148923    :
148924    :
148933    :
148934    :
148935    :
148936    :
148937    :
148938    :
148939    :
148951    :
148996    :

Now i want to gather all the "Tags" for which the "Result" is a number (buts
houldnt include the tag if the result is 0). The corresponding Result should
 go to another array. 

Considering the above example my array1 should contain 148312 and 148329 and
 array2 should contain 38160 and 44139 

How do i do this?

Thanks in advance

Regards
Sudhindra


Report this thread to moderator Post Follow-up to this message
Old Post
Ronald Yacketta
08-04-04 01:58 PM


Re: Choosing only numbers from the output
I think a hash is more apt for this problem, but changing to (an) array(s)
is not difficult at all.

The data as posted had white space trailing some of the digits after the
colons. I found this worked better for me. Your results may vary.
;-)

#!/usr/bin/perl
use warnings;
use strict;

my %tag;

while (<DATA> ) {
/^(\d+)\s*:\s*(\d+)/ or next;
$tag { $1 } = $2 unless $2 =~ /^0+$/;
}
while ( my($k, $v) = each %tag ) { print "$k ==> $v\n"; }

__DATA__
146603    :
147020    :
147155    : NONE
.
.
.


"Gunnar Hjalmarsson" <noreply@gunnar.cc> wrote in message
news:20040804101403.16584.qmail@onion.perl.org...

>      while (<DATA> ) {
>          my ($tag, $result) = split /[ :]+/;
>          if ($result =~ /^\d+$/ and $result > 0) {
>              ...



Report this thread to moderator Post Follow-up to this message
Old Post
Zeus Odin
08-04-04 08:56 PM


Re: Choosing only numbers from the output
Zeus Odin wrote:
> Gunnar Hjalmarsson wrote: 
>
> The data as posted had white space trailing some of the digits
> after the colons.

Yes, but how would that matter? Please read the second sentence in
"perldoc -f split".

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Report this thread to moderator Post Follow-up to this message
Old Post
Gunnar Hjalmarsson
08-05-04 01:55 AM


Re: Choosing only numbers from the output
You are correct. I did not properly look at the regex you passed to split.

"Gunnar Hjalmarsson" <noreply@gunnar.cc> wrote 
>
> Yes, but how would that matter? Please read the second sentence in
> "perldoc -f split".



Report this thread to moderator Post Follow-up to this message
Old Post
Zeus Odin
08-05-04 01:55 AM


Re: Choosing only numbers from the output
this is an interesting question in that its not about syntax or a specific
problem in perl.

here's the algorithm:

while there are lines to read from the input
read a line
split the line into two seperate strings around the colon
if both strings are numbers
append the first string into array1
append the second string into array2

that is "how you do this", or at least one way...=)

sudhindra k s wrote:

>
> Hi
>
> I have an output which looks like this:
>
> Tag       : Result
> 146603    :
> 147020    :
> 147155    : NONE
> 147160    :
> 147232    :
> 147243    :
> 147254    : none
> 147318    :
> 147341    : NONE
> 147430    : N/A
> 147708    :
> 147710    : 0000
> 147729    :
> 147768    :
> 147851    :
> 147921    :
> 147949    : 0000
> 147981    :
> 147983    :
> 147991    :
> 148007    : 0000
> 148080    :
> 148105    :
> 148200    :
> 148312    : 38160
> 148329    : 44139
> 148444    : NONE
> 148514    :
> 148573    : 0000
> 148697    :
> 148759    :
> 148919    :
> 148920    :
> 148921    :
> 148923    :
> 148924    :
> 148933    :
> 148934    :
> 148935    :
> 148936    :
> 148937    :
> 148938    :
> 148939    :
> 148951    :
> 148996    :
>
> Now i want to gather all the "Tags" for which the "Result" is a number
> (but shouldnt include the tag if the result is 0). The corresponding
> Result should go to another array.
>
> Considering the above example my array1 should contain 148312 and 148329
> and array2 should contain 38160 and 44139
>
> How do i do this?
>
> Thanks in advance
>
> Regards
> Sudhindra



Report this thread to moderator Post Follow-up to this message
Old Post
Christopher J. Bottaro
08-05-04 01:55 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:39 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.