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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |