AT first glance, I'm with LanX on this one (more or less).

If you wish to preserve the order found in the source file, put the hashes inside an array. If you don't care about order, or want them sorted, I'd put the arrays inside the hashes.

my %inpinf = (); while (my $inpbuf = <INPFIL>) { chomp $inpbuf; my ($inpkey, $inpval) = split /\s*\:\s*/, $inpbuf, 2; push @{$inpinf{$inpkey}}, $inpval; } C:\Steve\Dev\PerlMonks\P-2013-10-18@1437-DataMerge>type test2.dat Some Field : some value Another Field: 1234 Different One: 5678 Yet Another : foo Another Field: 9012 Different One: 3456 Yet Another : bar C:\Steve\Dev\PerlMonks\P-2013-10-18@1437-DataMerge>datamerge.pl test2. +dat Processing "test2.dat" %inpinf (C:\Steve\Perl/debug.pm:887(990)): [Another Field] => [ARRAY(0x4a8498)] [1234] [9012] [Different One] => [ARRAY(0x4bd6a8)] [5678] [3456] [Some Field] => [ARRAY(0x4a83f0)] [some value] [Yet Another] => [ARRAY(0x4bd768)] [foo] [bar]

Full code (apologies for using my home-spun debugmodule rather than Data::Dumperbut I haven't switched over to  Data::Dumperyet and wanted a fast way to show you the resulting data structure.)

#!/usr/bin/perl use strict; use warnings; use debug; foreach my $inpfnm (@ARGV) { print "Processing \"$inpfnm\"\n"; if (!open INPFIL, '<', $inpfnm) { print "ERROR: Cannot open input file \"$inpfnm\"\n"; } else { my $outfnm = $inpfnm . '.out'; if (!open OUTFIL, '>', $outfnm) { print "ERROR: Cannot open output file \"$outfnm\"\n"; } else { my %inpinf = (); while (my $inpbuf = <INPFIL>) { chomp $inpbuf; my ($inpkey, $inpval) = split /\s*\:\s*/, $inpbuf, 2; push @{$inpinf{$inpkey}}, $inpval; } close OUTFIL; &debug::predebugdumplist("\%inpinf", \%inpinf); } close INPFIL; } } exit; __END__

In reply to Re: Extracting and manipulating a range of lines by marinersk
in thread Extracting and manipulating a range of lines by joeymac

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.