Please advise me on how to structure my data. It's an embarrassingly basic Perl question but my hash of hashes isn't enough to hold the data I need to handle, and I don't know how to add the missing piece. The data structure needs to look like this
component => { version => "1.23.4.567", sources => { "filename1" => "checksumfile1", "filename2" => "checksumfile2", "filename3" => "checksumfile3", }
The data comes from a plain text file supplied in this format (no choice), one data item per line. (Filenames and checksum filenames are long URLS, so can't be conveniently placed on the same line but the source file URL line is immediately followed by the line with the URL for its checksum file):
component=HF version=NULL sourcefile=filename1 sourcesum=checksumfile1 sourcefile=filename2 sourcesum=checksumfile2 sourcefile=filename3 sourcesum=checksumfile3 component=SVM version=10.0.70.102 sourcefile=filename4 sourcesum=checksumfile4
My attempted code is like this:
use strict; use Data::Dump qw(dump); my %HoH = (); my $rec; my $component; while ( <DATA> ) { chomp; next unless m/^\S+/; if( m/^component=(.*)/ ) { $component = $1; $rec = {}; $HoH{$component} = $rec; } elsif( m/^version=(.*)/ ) { $rec->{"version"} = $1; } else { my ($key, $value) = split /=/; $rec->{$key} = $value; } } for my $component ( keys %HoH ) { print "$component: "; for my $key ( keys %{ $HoH{$component} } ) { print "$key=$HoH{$component}{$key} "; } print "\n"; } dump %HoH; __DATA__ component=HF version=NULL sourcefile=filename1 sourcesum=checksum1 sourcefile=filename2 sourcesum=checksum2 sourcefile=filename3 sourcesum=checksum3 component=SVM version=10.0.70.102 sourcefile=filename4 sourcesum=checksum4
Unfortunately (of course) the data for sources 1 and 2 is overwritten by source 3, so I think I need an array of source hashes, but I can't figure out how to code it thus this plea for help, kind monks.

In reply to how to include an array of hashes in a hash of hashes? by anadem

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.