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

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

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