Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

For loop Help

by Anonymous Monk
on Apr 02, 2004 at 15:31 UTC ( [id://342000]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi having some problems here.
This code is already inside of a for loop
####### for $x (0..$avarege->getLength-1) { ................ my($res_month, $res_day, $res_year); foreach my $key ($effectivedate[$x]) { chomp $key; ($res_month, $res_day, $res_year) = split/\//, $key; my @results = $key; print $#results; print "@results - <b>data found in $res_month * $res_day * $res_yea +r</b>"; if($res_month >= $res_month){print "^^ $res_month ^^";} } .....} #######

At the end I would like to print the line that has the most recent date.
Here a sample of the data.
Record 0: Coll 02/14/2003 Charles S. 2862.30 00000 ISSUE WWW Record 1: Coll 03/17/2003 Peter C. 392.50 00000 ISSUE WWW Record 2: Coll 07/25/2003 John K. 10.00 00000 ISSUE CCC

From this file I would get this line:
Record 2: Coll 07/25/2003 John K. 10.00 00000 ISSUE CCC
Need some help.....

Replies are listed 'Best First'.
Re: For loop Help
by esskar (Deacon) on Apr 02, 2004 at 15:49 UTC
    sub compare { my ($x, $y) = @_; my $date1 = join("", reverse split(/\//, $x)); my $date2 = join("", reverse split(/\//, $y)); return $date1 <=> $date2; } my @ordered = map { compare($a, $b) } @effectivedate; print $ordered[0];
Re: For loop Help
by neniro (Priest) on Apr 02, 2004 at 16:38 UTC
    Okay, it is ugly, but it works:
    #!/usr/bin/perl use strict; use warnings; my %result; while(<DATA>) { m#(\d{2})/(\d{2})/(\d{4})#; $result{$3. $2. $1} = $_; }; my $highest = $result{(sort keys %result)[-1]}; print $highest, "\n"; exit; __DATA__ Record 0: Coll 02/14/2003 Charles S. 2862.30 00000 ISSUE WWW Record 1: Coll 03/17/2003 Peter C. 392.50 00000 ISSUE WWW Record 2: Coll 07/25/2003 John K. 10.00 00000 ISSUE CCC
Re: For loop Help
by Anonymous Monk on Apr 02, 2004 at 23:12 UTC
    Is the most recent the last line in the file, like your sample data. If so, you could try File::ReadBackwards and only read and print the last line of the file & exit.
    use File::ReadBackwards ; # Object interface $bw = File::ReadBackwards->new( 'log_file' ) or die "can't read 'log_file' $!" ; while( defined( $log_line = $bw->readline ) ) { print $log_line ; last; # Only print the last line }
    HTH,

    Chris

Re: For loop Help
by eric256 (Parson) on Apr 02, 2004 at 15:41 UTC

    I missed the question in that. What part do you need help with?


    ___________
    Eric Hodges

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://342000]
Approved by herveus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-25 12:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found