in reply to Convert stirng to array of hashes
Gives:use strict; use warnings; use Data::Dumper; my $string = q({file => 'file1', desc => 'test'}, {file => 'file12', +desc => 'test2'}); my @array_of_hashes; my @hashes = $string =~ /(\{.*?\})/g; for my $hashstr (@hashes) { my @keys = $hashstr =~ /(\w+)\s*=>/g; my @values = $hashstr =~ /=>\s*\'(\w+)\'/g; my %hash; @hash{@keys} = @values; push @array_of_hashes, \%hash; } print Dumper(\@array_of_hashes);
$VAR1 = [ { 'desc' => 'test', 'file' => 'file1' }, { 'desc' => 'test2', 'file' => 'file12' } ];
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Convert stirng to array of hashes
by codeacrobat (Chaplain) on Aug 08, 2009 at 20:34 UTC | |
|
Re^2: Convert stirng to array of hashes
by Anonymous Monk on Aug 09, 2009 at 13:13 UTC | |
|
Re^2: Convert stirng to array of hashes
by courierb (Novice) on Aug 10, 2009 at 03:28 UTC |