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

cake_number;first_price cake_1;3; cake_2;4; cake_3;9; cake_4;7; cake_9;8;
and
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


In reply to hash table with three input files by steph_bow

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.