in reply to hash table with three input files

You can store each price in a list, and all the lists in a hash:
my %prices; # read all files: for $fn (@files){ open (my $file, '<', $fn) or die "Can't open '$fn' for reading: $!" +; while (<$file>){ chomp; my @items = split m/;/, $_; push @{$prices{$items[0]}, $items[1]; } } # print output: for my $k (sort keys %prices){ print "$k;", join(';', @{$prices{$k}}), $/; }

This works for an arbitrary number of files, but if some prices are only present in some files, the result doesn't show which prices comes form which file.

Replies are listed 'Best First'.
Re^2: hash table with three input files
by steph_bow (Pilgrim) on Feb 12, 2008 at 13:50 UTC

    Thanks a lot for your help ! moritz

    Concerning your remark to overcome the problem, could this work ? Thanks

    while (<$file>){ chomp; my @items = split m/;/, $_; if (defined($items[1])){ push @{$prices{$items[0]}, $items[1]; } else{ # add a ";" when there is a value void $prices{$items[0]} = "$prices{$items[0]}".";"; } }