A quick dissection of what is going on here:

On Mon, 2002-03-04 at 10:20, Nikola Janceski wrote:
> Yes you can do it with perl, and I suggest using hashes.
> 
> open(FILE, "$file");

This opens a file and associates it with FILE; you really should say
something like 'or die "Could not open $file:$!"' to make sure the file
opened.

> my %seen;

Creates a hash variable named %seen;

> while(<FILE>){

This loops while there are still lines left to read from FILE.  I would
have used just the <> operator to read from either the stdin or files
named on the command line and ditched the open above, but that is just a
style issue.

>       my ($item9, $item10) = (split /\|/, $_)[8,9];

This line is fairly complicated.  First we are splitting the line read
by the while above into a list using the '|' character as the
separator.  Then taking an array slice (a method of returning only a
subset of an array) that contains the ninth and tenth fields (remember
Perl uses zero based arrays).  Finally we are storing field nine into
$item9 and field ten into $item10.  

>       if(exists $seen{$item9}){

This bit of code checks to see if we have seen $item9 before by checking
to see if it exists in the hash %seen.  This would normally  be
shortened to "if ($seen{$item9}) {" since the value associated with any
key not stored in the hash is undef by default and undef is equivalent
to false, but we are storing field ten in it later and field ten can be
0 (which is also false).

>               if($itme10 > $seen{$item9}){
>                       # the new $item10 is larger than the last one seen

You would want to say 'print $_;' (or possibly jsut 'print;' since print
uses the default variable, $_, when there are no other arguments) here
to print the record.

>                       } else {
>                       # the contrary is true
>                       }
>               
>               } else {
>               $seen{$item9} = $itme10;

Here we are setting the value of the key $item9 to the value $item10 in
the hash named %seen.  This is also where you would want to print the
record since it is the first time we have seen $item9.  See above for
how to do that.

>               }
> close FILE;

This closes the file associated with FILE.  It is not completely
necessary since perl will close all open file handles on exit, but I
think it is good practice. 

> 
> -----Original Message-----
> From: Dave Adams [mailto:[EMAIL PROTECTED]]
> Sent: Monday, March 04, 2002 10:10 AM
> To: [EMAIL PROTECTED]
> Subject: Can I do this with perl?
> 
> 
> Hi there,
> 
> I have a problem which I would like to use perl to resolve, however I'm not
> sure if it is possible to do. 
> 
> I need to scan a file and check some conditions, first if field 9 is
> duplicated on 1 or more rows, then I need to check field 10 to see which is
> the greater value and then only print the whole row where field 10 is the
> greater, if field 9 is not a duplicate then print the whole row.
> An example of the data is below.
> 
> 28525|U2|4CY0|50|6775.15|2002-02-07|10461|321.43|102040724|102040773|
> 28526|U2|4CY0|25|3571.78|2002-02-07|6107|167.74|102040774|102040798|
> 28527|U2|4CY0|50|6930.3|2002-02-07|11376|324.12|102040774|102040823|
> 28528|U2|4CY0|25|4640.28|2002-02-07|4800|217.43|102040824|102040848|
> 28529|U2|4CY0|50|8432.05|2002-02-07|9023|392.03|102040824|102040873|
> 
> Dave
> 
> ----------------------------------------------------------------------------
> --------------------
> The views and opinions expressed in this email message are the sender's
> own, and do not necessarily represent the views and opinions of Summit
> Systems Inc.
> 
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
-- 
Today is Pungenday the 63rd day of Chaos in the YOLD 3168
You are what you see.

Missile Address: 33:48:3.521N  84:23:34.786W


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to