Prints:use feature qw(say); use Data::Dumper; my $data =<<'XML-type'; <item><key1>someValue</key1><key2>someValue</key2><key3>someValue</key +3> <key4>someValue</key4></item><item><key1>someValue</key1><key2>someVal +ue </key2><key3>someValue</key3><key4>someValue</key4></item><item> <key1>someValue</key1><key2>someValue</key2><key3>someValue</key3><key +4>someValue</key4></item> XML-type my @processed; local $/ = '</item>'; open STRH , '<', \($data); my @items = <STRH>; close STRH; @processed = map { my $item =$_; $item =~ s/<.?item>//g; push @processed, {$item =~ m { <([^>]+)>([^<]+)< }gx}; } @items; print Dumper \@processed;
For bonus points work out why there is an empty hash at the end.(I haven't got time just now!)$VAR1 = [ { 'key2' => 'someValue', 'key4' => 'someValue', 'key1' => 'someValue', 'key3' => 'someValue' }, { 'key2' => 'someValue ', 'key4' => 'someValue', 'key1' => 'someValue', 'key3' => 'someValue' }, { 'key2' => 'someValue', 'key4' => 'someValue', 'key1' => 'someValue', 'key3' => 'someValue' }, {} ];
In reply to Re: How do I create an array of hashes from an input text file?
by mrstlee
in thread How do I create an array of hashes from an input text file?
by MrSnrub
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |