Inexistence has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks, After days of messing around with perl mods, trying to get them to work... I gave up and am trying to code it myself as it seems simple enough to parse this EXAMPLE DATA (XML:Simple simply won't work on my host and I don't have access to add other mods)
?xml version <GeocodeResponse> <status>OK</status> <result> <type>street_address</type> <formatted_address>1600 Amphitheatre Pkwy</formatted_address> <address_component> <long_name>1600</long_name> <short_name>1600</short_name> <type>street_number</type> </address_component>
I'm looking to create this example from LUA, unless theres an easier way to do this of course
mytable = { [GeocodeResponse] = { [result] = { [type] = "street address", [formatted_address] = "1600 Amphitheatre Pkwy", [address_component] = { [long_name] = 1600, [short_name] = 1600, [type] = "street_number" } } } };
I'm trying to build an array from the table data above by reading line by line and creating the appropriate array tree (I'm not sure of terminology as i'm use to LUA) I know the below code is a mess, its as clean as i'm able to make it from tutorials on the interwebz.
if(length($content) >= 10 && $content =~ m/\<status\>OK\<\/status\>/) +{ my @lines = split /\n/, $content; $depth=0; %myarray = (); $arrayKey = {}; foreach my $line (@lines) { if($line =~ m/\?xml version/) { # DO NOTHING } else { if($line =~ m/\<(.*)\>(.*)\<\/.*\>/) { $arrayLoc = \%myarray; for ($i = 1; $i < $depth; $i++) { local $k=$arrayKey[$i]; if(!$arrayLoc->{$k}) { $arrayLoc->{$k}=() } $arrayLoc=$arrayLoc{$k}; } } elsif($line =~ /^\s{0,4}\<\/(.*)\>/) { $depth--; $arrayKey[$depth]=$1; } elsif($line =~ /^\s{0,4}\<(.*)\>$/) { $depth++; $arrayKey[$depth]=$1; } } } }
I'm sure its easy for you all, but not me Thank-you *bow*
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: recreating an array
by Anonymous Monk on Sep 08, 2011 at 21:00 UTC | |
by Inexistence (Acolyte) on Sep 08, 2011 at 21:10 UTC | |
|
Re: recreating an array
by charlesboyo (Beadle) on Sep 08, 2011 at 21:07 UTC | |
by Inexistence (Acolyte) on Sep 08, 2011 at 21:26 UTC | |
|
Re: recreating an array
by Anonymous Monk on Sep 09, 2011 at 16:18 UTC |