Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Generating a list of numbers from other lists

by Marshall (Canon)
on Jan 29, 2012 at 15:46 UTC ( [id://950584]=note: print w/replies, xml ) Need Help??


in reply to Generating a list of numbers from other lists

I made a hash out of the numbers to search for list. Then ran through the main list, kept the relevant matching lines, then sorted that list by the first number. While doing that I kept a running sum of each section in %sums. I wasn't sure what the output should look like.

Update: added the sum calculation. And I now see that there is some text that you want to skip, use a regex for that. Anyway you have another solution.

#!/usr/bin/perl -w use strict; $|=1; my @numberList = qw (20 30 50); #you get this from a file my %numberHash = map{$_ => 1}@numberList; my @result; my %sums; while (<DATA>) { chomp; my ($first, $second) = split; if ($numberHash{$first}) { push @result, $_; $sums{$first}+= $second; } } @result = sort{ my($firstA) = split(' ',$a); my($firstB) = split(' ',$b); $firstA <=> $firstB }@result; foreach (@result) { my ($first) = split; print "$_ $sums{$first}\n"; } #not sure what format you want output in =prints 20 23424 332444 20 56756 332444 20 82757 332444 20 83758 332444 20 09347 332444 20 76402 332444 30 91857 364101 30 92785 364101 30 89275 364101 30 90184 364101 50 29349 184748 50 71674 184748 50 83725 184748 =cut __DATA__ 10 23432 20 23424 60 45567 20 56756 30 91857 50 29349 10 93729 80 82374 20 82757 30 92785 50 71674 70 81747 20 83758 30 89275 10 19594 60 09214 20 09347 50 83725 90 91845 20 76402 30 90184

Replies are listed 'Best First'.
Re^2: Generating a list of numbers from other lists
by mbethke (Hermit) on Jan 29, 2012 at 17:07 UTC
    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 :)
      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://950584]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found