in reply to Multiple File handling and merging records from 2 files
A self-contained example solution using the HoH you were thinking of.
johngg@shiraz:~/perl/Monks > perl -Mstrict -Mwarnings -E ' open my $file1FH, q{<}, \ <<__EOD1__ or die $!; App1,4,Perl App2,8,Java App3,8,Java App4,4,PHP App5,8,C# __EOD1__ my %apps; while ( <$file1FH> ) { chomp; my( $key, $mem, $lang ) = split m{,}; $apps{ $key }->{ mem } = $mem, $apps{ $key }->{ lang } = $lang; } close $file1FH or die $!; open my $file2FH, q{<}, \ <<__EOD2__ or die $!; App1,1.5,2 App2,2.5,4 App3,2.8,4 App4,2.8,2 App5,2.8,2 __EOD2__ while ( <$file2FH> ) { chomp; my( $key, $cpu, $cores ) = split m{,}; $apps{ $key }->{ cpu } = $cpu; $apps{ $key }->{ cores } = $cores; } say for sort { $apps{ $a }->{ mem } <=> $apps{ $b }->{ mem } } grep { $apps{ $_ }->{ cores } eq q{2} } sort keys %apps;' App1 App4 App5
I hope this is useful.
Update: Corrected spelling mistake and added the following note:-
The reason I sort the keys before passing them into the grep is that the order returned by the keys function is essentially random. Since Perl's sort is "stable" it would not change the order in which "App1" and "App4" appeared as they have the same memory value so a result of
App4 App1 App5
could happen.
Cheers,
JohnGG
|
|---|