Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: Generating a list of numbers from other lists

by mbethke (Hermit)
on Jan 29, 2012 at 17:07 UTC ( [id://950600]=note: print w/replies, xml ) Need Help??


in reply to Re: Generating a list of numbers from other lists
in thread Generating a list of numbers from other lists

To avoid the expensive split in a sort block, you could separate the results in lists of their own using a second hash:
my (%result, %sums); while (<DATA>) { chomp; my ($first, $second) = split; if ($numberHash{$first}) { push @{$result{$first}}, $second; $sums{$first}+= $second; } } foreach my $first (sort { $a <=> $b } keys %result) { foreach my $second (@{$result{$first}}) { print "$first $second $sums{$first}\n"; } }
Just in case the OP ever wants to process tens of megabytes that way :)

Replies are listed 'Best First'.
Re^3: Generating a list of numbers from other lists
by Marshall (Canon) on Jan 29, 2012 at 18:45 UTC
    I certainly appreciate your point! And it is well taken.

    I try to modulate my response to the skill level reflected by the original question (with an imperfect heuristic). Sometimes I am more effective at this than at other times. The HoA (Hash of Array) syntax can often be a bit confusing for beginners. I just avoided this "what heck does push @{$result{$first}}"? mean by not doing that. Anyway, I think the collective "Monk Wisdom" has done well for this OP - there a several alternatives, all of which will probably work out just fine.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://950600]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-19 19:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found