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

hi perl-geeks,
sorry, to came up wich such a lame question, but i'm really stucked and can't find a solution (searched for a similar snippet on permonks - no success).
i've a file with two lines (tab delimetered, 1st string IP-subnet, 2nd an integer).
Example:
192.168.1 23
192.168.1 14
192.168.4 8
192.168.3 13
192.168.3 12

so i would like to sum all integer for a specific subnet.
Output should be:
192.168.1 37
192.168.4 8
192.168.3 25


thanks in advance
geri

Replies are listed 'Best First'.
Re: compare and sum hash from array
by dragonchild (Archbishop) on Dec 20, 2004 at 15:13 UTC
    Can you show us your attempt(s) and tell us why they haven't worked? We tend to help better if we see you've tried something. Otherwise, we end up dismissing questions as students trying to get us to do their homework for them.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: compare and sum hash from array
by zejames (Hermit) on Dec 20, 2004 at 15:13 UTC

    Update : as this looks like homework, change my solution to be more cryptic, and from which I'm quite sure the OP will be able to learn from :

    As you say in your title, you must use a hash:

    m!([\S]+)\t(\d+)(?{$hash{$1} +=$2})! while (<DATA>); print "$_ -> $hash{$_}\n" foreach (keys %hash); __DATA__ 192.168.1 23 192.168.1 14 192.168.4 8 192.168.3 13 192.168.3 12

    However, you question looks like a homework. I would be better if you provided your work so that we give you direction or better code if needed.


    --
    zejames

    PS : anyone interested into a obfuscated context on this request ?

Re: compare and sum hash from array
by perl_lamer (Initiate) on Dec 22, 2004 at 07:46 UTC
    thank you, zejames for your immediate response ! man, perl really rulez !!!
    i was playing aroung with a loop to establish this
    task and then i see your short solution - cool !

    BTW: this was not a homework !