in reply to File sort and values in file name

Try this...
#!/usr/bin/perl use strict; my %records; while (<DATA>) { chomp; my @values = split( "_" ); next unless $values[2] eq 'max'; $records{ $values[0] } = $values[1] if ( $records{ $values[0] } > $values[1] || ! $records{ $values[0] } ); } print map { "$_ => $records{$_} \n" } keys %records; __DATA__ 111aaa222ccc324567fed54333221235_1.04 111aaa222ccc324567fed54333221235_1.05_max 111aaa222ccc324567fed54333221235_0.98_min 111aaa222ccc324567fed54333221235_1.02_max 111aaa222ccc324567fed54333221235_0.21 777aaa222ccc324567fed54333221235_1.04 777aaa222ccc324567fed54333221235_1.07_min 777aaa222ccc324567fed54333221235_1.04_max

Replies are listed 'Best First'.
Re^2: File sort and values in file name
by Marshall (Canon) on Sep 19, 2009 at 02:31 UTC
    I liked your code, but I am averse to using [$index] in Perl code unless it is needed. I would give names to the list on the left as result of a split. A chomp() would make the split regex a bit easier, but no biggie. I am having a "brain cramp" right now on how to code the hash assignment statement better. My main point is to avoid this numeric $index stuff.
    #!/usr/bin/perl -w use strict; my %records; while (<DATA>) { my ($md5,$version,$end) = split(/[_\n]+/,$_); next unless ($end eq 'max'); $records{$md5} = $version unless ( exists $records{$md5} && $records{$md5}< $version) ; } foreach my $md5 (sort keys %records) { print $md5,"_",$records{$md5},"_max\n"; } #Prints: #111aaa222ccc324567fed54333221235_1.02_max #777aaa222ccc324567fed54333221235_1.04_max __DATA__ 111aaa222ccc324567fed54333221235_1.04 111aaa222ccc324567fed54333221235_1.05_max 111aaa222ccc324567fed54333221235_0.98_min 111aaa222ccc324567fed54333221235_1.02_max 111aaa222ccc324567fed54333221235_0.21 777aaa222ccc324567fed54333221235_1.04 777aaa222ccc324567fed54333221235_1.07_min 777aaa222ccc324567fed54333221235_1.04_max