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*


In reply to recreating an array by Inexistence

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.