john.tm has asked for the wisdom of the Perl Monks concerning the following question:

I run a report between 2 csv files, the last bit i wish to do check is to add matching elemants of the 2 arrays (built up of unique values and occurances) together. but i can't work out how. snippet of code
INPUT output is jon 22 james 12 ken 22 jack 33 jim 11 harry 7 dave 9 grant 12 matt 74 malc 12
INPUT1 output is jon 2 james 1 ken 8 jack 5 jim 1 harry 51 dave 22
output i want is
jon 24 james 13 ken 30 jack 38 jim 12 harry 58 dave 31 grant 12 matt 74 malc 12
code i have so far is
my %seen; seek INPUT, 0, 0; while (<INPUT>) { chomp; my $line = $_; my @elements = split (",", $line); my $col_name = $elements[1]; #print " $col_name \n" if ! $seen{$col_name}++; } while ( my ( $col_name, $times_seen ) = each %seen ) { my $loc_total = $times_seen * $dd; print "\n"; print " $col_name \t\t : = $loc_total"; printf OUTPUT "%-34s = %15s\n", $col_name , " $loc_total "; } ############## ################### my %seen2; seek INPUT1, 0, 0; while (<INPUT1>) { chomp; my $line = $_; my @elements1 = split (",", $line); my $col_name = $elements1[1]; my $col_type = $elements1[5]; $seen2{$col_name}++ if $col_type eq "YES"; } while ( my ( $col_name, $times_seen2 ) = each %seen2 ) { my $loc_total = $times_seen2 ; print "\n $col_name \t\t= $loc_total"; printf OUTPUT "%-34s = %15s\n", $col_name , $times_seen2 ; } close INPUT;

Replies are listed 'Best First'.
Re: perl to add results of 2 arrays from 2 files together.
by hippo (Archbishop) on Aug 26, 2014 at 12:45 UTC

    You appear to have the data in two hashes: %seen and %seen2 and you want to merge them. Therefore, try studying the FAQ response: How do I merge two hashes?. Does this provide the answer you require?

      can you merge hashes fron 2 different files?
        Yes. The hashes' data resides in memory; where it came from originally is irrelevant.
        You actually don't even need to merge hashes if you consider that you can do the whole thing with one single hash. Just read the first file, store the values in a hash, read the second file, add the values to those already in the hash for each key. And you're basically done.
Re: perl to add results of 2 arrays from 2 files together.
by LanX (Saint) on Aug 26, 2014 at 13:19 UTC
    that's what you are looking for?

    DB<253> @a{a..c}=1..3 => (1, 2, 3) DB<254> @b{b..d}=6..8 => (6, 7, 8) DB<255> $c{$_}=$a{$_}+$b{$_} for (keys %a,keys %b) => "" DB<256> \%c => { a => 1, b => 8, c => 10, d => 8 }

    NB: you might need to set no warnings 'uninitialized' in a scope surrounding line 255.

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

    update

    I just noticed that, while producing correct results, this code will be a bit redundant, cause it repeats operations (here for b and c)

    If this is really an issue, do an exists $c{$_} check before adding or apply uniq from List::MoreUtils.

Re: perl to add results of 2 arrays from 2 files together.
by LanX (Saint) on Aug 26, 2014 at 13:48 UTC
    TIMTOWTDI: =)
    DB<270> @a{a..c}=1..3 => (1, 2, 3) DB<271> @b{b..d}=6..8 => (6, 7, 8) DB<272> @c{keys %a, keys %b}=() ;# not needed => (undef, undef, undef, undef, undef, undef) DB<273> $c{$_} += $a{$_} for keys %a => "" DB<274> $c{$_} += $b{$_} for keys %b => "" DB<275> \%c => { a => 1, b => 8, c => 10, d => 8 }

    edit

    line 272 is actually not needed and code doesn't cause warnings! :)

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

Re: perl to add results of 2 arrays from 2 files together.
by AppleFritter (Vicar) on Aug 26, 2014 at 15:43 UTC
    LanX has already given some good answers, so I'll just add that I'd advise against trying to do your own CSV handling. Use Text:CSV - it'll gracefully handle files that a naive approach WILL get wrong. Don't reinvent the wheel without a compelling reason.
      > I'd advise against trying to do your own CSV handling

      While "his" code tries kind of CSV handling (splitting on comma) his "data" doesn't have comma separation.

      I'd say he C&P-ed googled code¹ to pretend own efforts! :-|

      Cheers Rolf

      (addicted to the Perl Programming Language and ☆☆☆☆ :)

      ¹) at one point he also reads the fifth column and compares it with "Yes" ?!?

        While "his" code tries kind of CSV handling (splitting on comma) his "data" doesn't have comma separation.

        I'd say he C&P-ed googled code¹ to pretend own efforts! :-|

        Hmm, I don't think that's the case; he just didn't share his actual input data. I actually began coding a "clean" solution that he'd be able to use as a starting point, but stopped when I realized that there was no sample data to be found to test it against. Trying to reverse-engineer what it might look like would've been error-prone, and besides, like any good programmer, I'm lazy. (Which is not to say I'm a good programmer, mind you.)

        The data from INPUT and INPUT1 is just an example of output saved to a text file. you are quite right 'while INPUT1' i am doing a count on all unique values in 'column2' that have a 'YES' in 'column6'