Inexistence has asked for the wisdom of the Perl Monks concerning the following question:
I've been working through this, trying to follow the recommendations and reading recommended information in books etc. I hope this is a vast improvement in both presentation and coding style over my initial posting
My Array Reference keeps wiping itself no matter what I do, so i'm doing something wrong obviously. I suspect i'm incorrectly using the reference in the loop, but... Also, if there is a proper method for me to adjust and repost this, please advise
As always *bow* and thank-you for your previous help
A basic XML example with proper formatting I think
<?xml version="1.0" encoding="UTF-8" ?> <GeocodeResponse> <status>OK</status> <result> <type>postal_code</type> <formatted_address>Edmonton, BC V2M 4M7, Canada</formatted +_address> <address_component> <long_name>V2M 4M7</long_name> <short_name>V2M 4M7</short_name> <type>postal_code</type> </address_component> <geometry> <location> <lat>29.1153940</lat> <lng>-133.8394480</lng> </location> </geometry> </result> </GeocodeResponse>
The code from what I can grasp so far (Someone previously supplied me with basic XML ripping code, however it didn't parse correctly, so I improved this method considerably before reposting it. I'm limited in XML mods, so this is how i'm making sure I can run this on ANY server without permission)
print "<br>Content-type: text/html \n\n"; my @lines = split /\n/, $content; #--content = above XML data my $depth=0; my @myarray; my $currVar; #--Declare/Set foreach my $line (@lines) { if($line =~ m/\?xml version/) { # DO NOTHING as this is useless header info } elsif($line =~ m/\<(.*)\>(.*)\<\/.*\>/) { #--THIS is DATA $currVar=\$myarray; #--Create reference to main array for ($i = 1; $i <= $depth; $i++) { #--Create the variable +reference $currVar=\@currVar[$arrayKey[$i]]; } $currVar[$1]=$2; } elsif($line =~ /^\s{0,4}\<\/(.*)\>/) { #--Array depth decrea +se $depth--; } elsif($line =~ /^\s{0,4}\<(.*)\>$/) { #--Array depth decr +ease $depth++; $arrayKey[$depth]=$1; #--Keep track of the variable names +according to depth } } print "<br>STATUS = ".$myarray[GeocodeResponse][status]." <---THIS + should say OK";
Expected output as array: (I hand typed this, but it should be close) Ok, Anonymous Monk told me this isn't even close to Perl format, so i'm making another version below it that I hope is Perl
$myarray=[GeocodeResponse]=[ status="OK", result=[ type="postal_code", formatted_address="Edmonton, BC V2M 4M7, Canada", address_component=[ long_name="V2M 4M7", short_name="V2M 4M7", type="postal_code" ], geometry=[ location=[ lat=29.1153940, lng=-133.8394480 ], ], ], ]
Hopefully Perl format... based on "Modern Perl", but i'll probably get it wrong too. Need to learn it though, so I don't mind at all being told whats wrong or ridiculed a little ;) Perl variables are apples to oranges compared to LUA
# creates a single array, not an array of arrays my @array_of_arrays = ( 1 .. 10, ( 11 .. 20, ( 21 .. 30 ) ) );
@array_of_arrays = ( status="OK", result=( type="postal_code", formatted_address="Edmonton, BC V2M 4M7, Canada", address_component=( long_name="V2M 4M7", short_name="V2M 4M7", type="postal_code" ), geometry=( location=( lat=29.1153940, lng=-133.839448 ) ) ) )
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Working through it...
by chromatic (Archbishop) on Sep 10, 2011 at 23:12 UTC | |
by Inexistence (Acolyte) on Sep 11, 2011 at 00:00 UTC | |
by chromatic (Archbishop) on Sep 11, 2011 at 01:29 UTC | |
by Inexistence (Acolyte) on Sep 11, 2011 at 21:14 UTC | |
by Inexistence (Acolyte) on Sep 11, 2011 at 22:35 UTC | |
by chromatic (Archbishop) on Sep 12, 2011 at 04:56 UTC | |
| |
|
Re: Working through it...
by Khen1950fx (Canon) on Sep 11, 2011 at 01:26 UTC | |
by Anonymous Monk on Sep 11, 2011 at 01:36 UTC | |
|
Re: Working through it...
by Anonymous Monk on Sep 10, 2011 at 22:46 UTC | |
by Inexistence (Acolyte) on Sep 10, 2011 at 23:07 UTC | |
by chromatic (Archbishop) on Sep 10, 2011 at 23:13 UTC |