Hello Perlseeker_1
How about using dispatch table? Sometimes I see monks use it.

use strict; use warnings; use Data::Dumper; sub kv1{ my @flds=split(/\s+/, $_[0]); return( $flds[2], $flds[3] ); } sub kv2{ my @flds=split(/\s+/, $_[0]); return( $flds[1] . $flds[2], $flds[3] ); } my ($line,$fname, %data,%all, %kv_func); %kv_func=( #dispatch table 'file 1' => \&kv1, 'file 2' => \&kv1, 'file 3' => \&kv1, 'file 4' => \&kv1, 'file 5' => \&kv2, ); while( <DATA> ){ if (my $num = /^file/ .. /^\n/ ){ # test them with flip-flop chomp($line=$_); if( /^(file\s*\d+)/ ){ #begin of block $fname = $1; }elsif ($num =~ /E0/ ){ #end of block $all{$fname}={%data}; %data=(); } else { ##data lines my ($k,$v) = $kv_func{$fname}->($line); $data{$k}= exists($data{$k}) ? $data{$k}+$v : $v; } } } print Dumper \%all; __DATA__ file 1 2 1 AB 1.0 2 2 AB-1 2.0 4 1 AB-4 1.0 4 2 AB-6 2.0 a never comes here... file 2 21 01 AB 4.0 22 12 AB-1 6.0 41 44 AB-4 9.0 42 46 AB-6 4.0 42 46 AB-6 4.0 file 3 01 10 AB 4.0 20 02 AB-1 6.0 40 04 AB-4 9.0 02 26 AB-6 4.0 file 4 56 01 AB 4.0 56 12 AB-1 6.0 65 44 AB-4 9.0 65 46 AB-6 4.0 file 5 1 A B 12.00 2 C D 13.00 3 E F 11.00 4 G H 22.00


In reply to Re^3: Perl - Hashes by remiah
in thread Perl - Hashes by Perlseeker_1

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.