in reply to Re: Complicated Hash with array
in thread Complicated Hash with array

That works great. I will need to stare at that for a while to process it all though.

Any suggestions on a way to parse through the data line by line (of the DATA rows) by calling it by the item name? Something like below, although this obviously doesn't work with the current hash, this is what I envisioned but am unsure of how to accomplish it:
foreach my $data (@datalineshasharraything) { print "Item1 is ", $data{'ItemName1'}; print "Item2 is ", $data{'ItemName2'}; }
Not sure if that is doable, as it is beyond my scope of Perl understanding. Any suggestions? Thanks


Michael Jensen

Replies are listed 'Best First'.
Re^3: Complicated Hash with array
by Hofmator (Curate) on Aug 08, 2006 at 12:47 UTC
    You can accomplish that inside the for (@listings) loop of GrandFather's solution by using a hash slice.
    for my $listing (@listings) { my @list = grep {length} split '~', $listing; my %data; @data{@keys} = @list; print "Item1 is ", $data{'ItemName1'}, $/; print "Item2 is ", $data{'ItemName2'}, $/; push @{$hoa{$keys[$_]}}, $list[$_] for 0..$#keys; }

    -- Hofmator

      Works perfectly! Thanks Grandfather and Hofmator!!

      The full code is included here for ease...
      #!/usr/bin/perl -w use strict; use Data::Dumper; my $rawdata = qq| <COLUMNS>~ItemName1~ItemName2~ItemName3~ItemName4~ItemName5~</COLUMNS> <DATA>~Apple~Orange~Banana~Pear~Watermelon~</DATA> <DATA>~Blue~Red~Yellow~Brown~Purple~</DATA> <DATA>~Uno~Dos~Tres~Cuatro~Cinco~</DATA> |; my @datalines = grep {length} split "\n",$rawdata; my @listings; my $header; foreach my $line (@datalines) { if ($line =~ /^<COLUMNS>(.+)<\/COLUMNS>/) { $header = $1; } elsif ($line =~ /^<DATA>(.+)<\/DATA>/) { push(@listings, $1); } } my %hoa; my @keys = grep {length} split '~', $header; for my $listing (@listings) { my @list = grep {length} split '~', $listing; push @{$hoa{$keys[$_]}}, $list[$_] for 0..$#keys; } for my $listing (@listings) { my @list = grep {length} split '~', $listing; my %data; @data{@keys} = @list; print "Item1 is ", $data{'ItemName1'}, ", "; print "Item2 is ", $data{'ItemName2'}, ", "; print "Item3 is ", $data{'ItemName3'}, "\n"; }


      Michael Jensen