in reply to Complicated Hash with array

Without any error checking this works for you sample:

use warnings; use strict; 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; } print "$_ => @{$hoa{$_}}\n" for sort keys %hoa;

Prints:

ItemName1 => Apple Blue Uno ItemName2 => Orange Red Dos ItemName3 => Banana Yellow Tres ItemName4 => Pear Brown Cuatro ItemName5 => Watermelon Purple Cinco

DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: Complicated Hash with array
by inblosam (Monk) on Aug 08, 2006 at 12:34 UTC
    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
      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