Just a quick piece of code that seems to be doing what you want:
use strict; use warnings; use Data::Dumper; my $file1 = "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},{ }"; open my $FH1, "<", \$file1 or die "cannot open $file1"; my %file1Hash; while (my $line = <$FH1>) { my ($item, $period) = split /,/, $line; $file1Hash{$item} = $period; } close $FH1; print Dumper \%file1Hash; my $file2 = "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"; open my $FH2, "<", \$file2 or die "cannot open $file2"; while (my $line = <$FH2>) { chomp $line; my ($item, $slew) = (split /,/, $line)[0,4]; next unless $file1Hash{$item}; # skip the line if the period is no +t defined or if it is 0 my $skew = 100 * $slew / $file1Hash{$item}; printf "%s,%d,%.8f\n", $line, $file1Hash{$item}, $skew; }
Note that I embedded the input data into the code for simplicity of the test.

And an sample execution (at the command line):

$ perl -e 'use strict; > use warnings; > use Data::Dumper; > > > my $file1 = "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},{ }"; > > open my $FH1, "<", \$file1 or die "cannot open $file1"; > my %file1Hash; > while (my $line = <$FH1>) { > my ($item, $period) = split /,/, $line; > $file1Hash{$item} = $period; > } > close $FH1; > print Dumper \%file1Hash; > > my $file2 = "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"; > > open my $FH2, "<", \$file2 or die "cannot open $file2"; > while (my $line = <$FH2>) { > chomp $line; > my ($item, $slew) = (split /,/, $line)[0,4]; > next unless $file1Hash{$item}; # skip the line if the period is +not defined or if it is 0 > my $skew = 100 * $slew / $file1Hash{$item}; > printf "%s,%d,%.8f\n", $line, $file1Hash{$item}, $skew; > }' $VAR1 = { 'dt_pp50' => '200.11', 'an_en11' => '200.00', 'br_gh13' => '140.09', 'er_tk02' => '305.47', 'ce_oy74' => '300.05', 'ef_yb41' => '200.05' }; dt_pp50,0,0,2,0.000,0.000,0,0.000,200,0.00000000 er_tk02,0,2,3,0.002,0.004,0,0.001,305,0.00065473 ef_yb41,0,1,5,0.000,0.000,0,0.000,200,0.00000000
Also note that I used split to keep closer to your code, but it would make sense to use Text::CSV_XS or Text::CSV as recommended earlier by other monks, especially if your input data gets more complicated.

HTH.


In reply to Re^3: How to add a new column into my csv file? by Laurent_R
in thread How to add a new column into my csv file? 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.