If you read the "looking for" file into an array you can use it to construct a regular expression that selects only the lines you want and captures the two numeric elements. It also means you don't have to do any sorting because the array is already in the order you desire. I use a single HoH (hash of hashes) to hold the lines and also the sum of the second terms.

use strict; use warnings; use 5.010; use Data::Dumper; open my $lookFH, q{<}, \ <<EOD or die qq{open: << HEREDOC: $!\n}; 20 30 50 EOD chomp( my @lookFor = <$lookFH> ); my $lookForRE = do{ local $" = q{|}; qr{^(@lookFor)\s+(\d+)} }; my %accumulate; open my $nosFH, q{<}, \ <<EOD or die qq{open: << HEREDOC: $!\n}; 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 EOD while ( <$nosFH> ) { next unless m{$lookForRE}; chomp; push @{ $accumulate{ $1 }->{ lines } }, $_; $accumulate{ $1 }->{ sum } += $2; } foreach my $group ( @lookFor ) { say join q{ }, $_, $accumulate{ $group }->{ sum } for @{ $accumulate{ $group }->{ lines } } } print Data::Dumper->Dumpxs( [ \ %accumulate ], [ qw{ *accumulate } ] );

Here is the output. I have included the Data::Dumper->Dumpxs() representation of the hash to show the resulting data structure.

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 %accumulate = ( '50' => { 'lines' => [ '50 29349', '50 71674', '50 83725' ], 'sum' => '184748' }, '30' => { 'lines' => [ '30 91857', '30 92785', '30 89275', '30 90184' ], 'sum' => '364101' }, '20' => { 'lines' => [ '20 23424', '20 56756', '20 82757', '20 83758', '20 09347', '20 76402' ], 'sum' => '332444' } );

I hope this is helpful.

Cheers,

JohnGG


In reply to Re: Generating a list of numbers from other lists by johngg
in thread Generating a list of numbers from other lists by mlebel

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.