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

Help Monks! i have a text file with a two columns of numbers i'm trying to sum. Example
12345 99 12345 99 23432 99 54567 20
i'm just trying to get the total for each number in the left column based on the right.
12345 = 198 23432 = 99 54567 = 20
??? and suggestions? Thanks.

Edit kudra, 2002-05-10 Changed title

Replies are listed 'Best First'.
Re: Stressed
by Joost (Canon) on May 10, 2002 at 17:04 UTC
    how about

    for (<>) { chomp; /(\d+)\s+(\d+)/ or next; $total{$1} += $2; } for (sort { <=> } keys %total) { print "$_ = $total{$_}\n"; }
    -- Joost downtime n. The period during which a system is error-free and immune from user input.
      Thanks alot. Worked like a champ. p.s. I love the donwtime def. too. :-)
Re: Stressed
by hopes (Friar) on May 10, 2002 at 17:31 UTC
    Upps! I'm not so fast... an oneliner:
    perl -e 'while (<>){@a=split;$s{$a[0]}+=$a[1] if $a[1]}print "$_=$s{$_ +}$/"for sort keys %s' texto.txt [fpalacios@linuxtest fpalacios]$ cat texto.txt 12345 99 12345 99 23432 99 54567 20 [fpalacios@linuxtest fpalacios]$ perl -e 'while (<>){@a=split;$s{$a[0] +}+=$a[1] if $a[1]}print "$_=$s{$_}$/"for sort keys %s' texto.txt 12345=198 23432=99 54567=20 [fpalacios@linuxtest fpalacios]$


    Hopes
    $_=$,=q,\,@4O,,s,^$,$\,,s,s,^,b9,s, $_^=q,$\^-]!,,print