Hi all,

So this is the files that i have. File 1 has like 400+ lines while file2 has only 200+ lines. I store column1 in file2 as key and the rest as values. I wanted to output the file as below by copying the lines that are present in file 2 and file 1.

File1: Item,Time,Pattern ,Attributes,From an_en11,200.00,{0 133},{ } br_gh13,140.09,{0 59},{ } ce_oy74,300.05,{0 230},{int_43} dt_pp50,200.11,{0 122},{ } er_tk02,305.47,{0 220},{ } ef_yb41,200.05,{0 233},{ } File2: Item,Sink,Buffer,Cell,Slew,Path,Violation,Area dt_pp50,0,0,2,0.000,0.000,0,0.000 er_tk02,0,2,3,0.002,0.004,0,0.001 ef_yb41,0,1,5,0.000,0.000,0,0.000 Output : Item,Sink,Buffer,Cell,Slew,Path,Violation,Area,Time dt_pp50,0,0,2,0.000,0.000,0,0.000,200.11 er_tk02,0,2,3,0.002,0.004,0, 0.001,305.47 ef_yb41,0,1,5,0.000,0.000,0,0.000,200.05

So this is the code that I had generate but when I run it, it says that the $keys,$value1,$value2 and $value3 are not initialized. Anyone know how to fix this?

my %file1Hash; my $value4; open my $file1, "<","design.rpt.csv" or die $!; open my $file2, "<","summary.rpt.csv"or die $!; open my $outfile_1, ">", "combined.rpt.csv" or die $!; while(<$file1>){ my($line) = $_; chomp $line; my($key,$value1,$value2,$value3) = $line =~ /(\w+),(\d+.\d+),(.\d+ +\s+\d+.\d+.)/g; $value4 = "$value1,$value2,$value3,"; push @{$file1Hash{$key}}, $value4; } while(<$file2>){ my ($line) = $_; chomp $line; my($key,$value1,$value2,$value3,$value4,$value5,$value6,$value7) = + $line =~ /(\w+|\S+),(\d+),(\d+),(\d+),(\d+.\d+),(\d+.\d+),(\d+),(\d+ +.\d+)/g ; if (exists $file1Hash{$key}) { print $file1Hash{$key}.",".$line."\n"; } else { # print $line."\n"; } } close $file1; close $file2; close $outfile_1; exit 0;

I don't know what is the problem and how to solve this. Please Help.


In reply to Combining Files using Hash by chaney123

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.