in reply to Merging two list with simple operation
Try this:
#!/usr/bin/perl use strict; use warnings; my %HASH; open(F1,"file1"); while (<F1>) { chomp; my ($k,$v) = split /\s+/,$_,2; $HASH{$k} = $v; } close(F1); open(F2,"file2"); while (<F2>) { chomp; my ($k,$v) = split /\s+/,$_,2; if (exists $HASH{$k} ) { $HASH{$k} *= $v; }else{ $HASH{$k} = $v; } } close(F2); # write output to file open(OUT,">newfile"); while (my ($k,$v) = each %HASH) { print OUT "$k $v\n"; } close(OUT);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Merging two list with simple operation
by fanticla (Scribe) on Jul 31, 2010 at 19:08 UTC | |
|
Re^2: Merging two list with simple operation
by rir (Vicar) on Aug 04, 2010 at 21:01 UTC |