steph_bow has asked for the wisdom of the Perl Monks concerning the following question:
Hello, Dear Monks,
I wish to construct an output file which merges the data from three input files. I did it with two input fils by using a hash table but I don't know how to do it with three files.
My two input files are excel csv files which have the following form
andcake_number;first_price cake_1;3; cake_2;4; cake_3;9; cake_4;7; cake_9;8;
cake_number;second_price cake_1;5; cake_2;5; cake_3;5; cake_6;9; cake_10;1;
Here is the output file (I have not tested yet)
cake_number;frist_price;second_price; cake_1;3;5; cake_2;4;5; cake_3;9;5;
Here is my program with two input files
my %hash_table_1; open(INFILE_1, "<$infile_1"); while (my $line_1 = <INFILE_1>){ my @Elements_1 = split ';', $line_1; chomp($Elements_1[1]); $hash_table_1{$Elements_1[0]} = $Elements_1[1]; } my %hash_table_2; open(INFILE_2, "<$infile_2"); while (my $line_2 = <INFILE_2>){ my @Elements_2 = split ';', $line_2; chomp($Elements_2[1]); $hash_table_2{$Elements_2[0]} = $Elements_2[1]; } my @KEYS_1 = keys(%hash_table_1); my @KEYS_2 = keys(%hash_table_2); my $key_1; foreach $key_1(@KEYS_1){ if ( defined $hash_table_2{$key_1}){ print OUTFILE "$key_1;$hash_table_1{$key_1};$hash_table_2{$key_1}\ +n"; } } close OUTFILE; close INFILE_1; close INFILE_2;
I do not know how to do it if I add a third column "third price". Could you give me some advice ? Thanks a lot
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: hash table with three input files
by moritz (Cardinal) on Feb 11, 2008 at 16:17 UTC | |
by steph_bow (Pilgrim) on Feb 12, 2008 at 13:50 UTC | |
|
Re: hash table with three input files
by svenXY (Deacon) on Feb 11, 2008 at 16:16 UTC | |
|
Re: hash table with three input files
by j1n3l0 (Friar) on Feb 11, 2008 at 17:08 UTC |