Prl_learner has asked for the wisdom of the Perl Monks concerning the following question:

hello,

I new perl learner, so forgive me if my question seemed basic.

I need a perl script to look up the unique elements in array and sum the corresponding values. let me make it clear

here is how my input file will look like:
AA 12 bb 34 AA 76 cc 98 dd 76 bb 98
Note: the file is tab delimited

I need the script to give me the following output:

AA 88 bb 132 cc 98 dd 76
the input is like a table of two columns or more. I don't why it appeared as a line in my question even thought I entered it as table. so basically the first column contain (AA, bb, AA, cc, dd, bb) and the second column contain (12, 34, 76, 98, 76, 98)

Can anyone help me with writing this script?

Replies are listed 'Best First'.
Re: find unique elments and sum the corresponding values
by linuxer (Curate) on Feb 11, 2009 at 23:20 UTC

    Your request would be clearer, if you had used code tags. see How do I post a question effectively?.

    My suggestion:

    #! /usr/bin/perl -l use strict; use warnings; my %sum_of; # read linewise from filehandle while ( my $line = <DATA> ) { # remove linebreak; for details, see: perldoc -f chomp chomp $line; # split at whitespace characters; see: perldoc -f split # change \s to \t if only tabs are allowed; # remove + from pattern, if only one whitespace separates the column +s my ( $name, @fields ) = split m{\s+}, $line; # support multiple columns; sum up the fields per column for my $idx ( 0 .. $#fields ) { # create a Hash of Arrays and store the sums; see: perldoc perldsc $sum_of{ $name }->[$idx] += $fields[$idx]; } } # print result for my $name ( keys %sum_of ) { print join "\t", $name, @{ $sum_of{$name} }; } # __DATA__ AA 12 bb 34 AA 76 cc 98 dd 76 bb 98

    update

    • comments extended; fixed syntax error in 'print join()'
      Hi..many thanks for the reply, I couldn't understand the main part of the script, the while loop. However, when I exscuted the script I got this message: Global symbol "$sum_of" requires explicit package name at SortSum.Pl line 22. Execution of SortSum.Pl aborted due to compilation errors How can this be fixed? and if possible can you explain the script to me so I can learn? thanks

        You really need to spend some time reading documentation. If you haven't a copy of 'Learning Perl' you should at least read through the Getting Started with Perl section in Tutorials.


        True laziness is hard work

        Don't know if you already saw my updated code. I added some comments and fixed that syntax error. Had a bad typo in the "print result" section.

Re: find unique elments and sum the corresponding values
by JavaFan (Canon) on Feb 12, 2009 at 00:09 UTC
    map{split;${"''$_[0]'"}+=$_[1]}<DATA>;map{/^'(.*)'$/&&say$1,$",${"''$1 +'"}}%::; __DATA__ AA 12 bb 34 AA 76 cc 98 dd 76 bb 98 __END__ bb 132 dd 76 cc 98 AA 88
    Quiz: explain all the quotes.
Re: find unique elments and sum the corresponding values
by GrandFather (Saint) on Feb 11, 2009 at 23:17 UTC

    How about you show us some code you have tried? Why didn't you read the site documentation that tells you how to format your node?


    True laziness is hard work
      Look Ma,

      I've found a newline! :P


      holli

      When you're up to your ass in alligators, it's difficult to remember that your original purpose was to drain the swamp.