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
In reply to Re: Complicated Hash with array
by GrandFather
in thread Complicated Hash with array
by inblosam
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |