in reply to Working through it...

What output do you get?

            $currVar=\$myarray; #--Create reference to main array

Do you mean instead $currVar = \@myarray;?

What do you expect this to do?

for ($i = 1; $i <= $depth; $i++) { #--Create the variable +reference $currVar=\@currVar[$arrayKey[$i]]; } $currVar[$1]=$2;

This is incredibly fragile to whitespace changes, which are generally meaningless in XML:

elsif($line =~ /^\s{0,4}\<\/(.*)\>/) { #--Array depth decrease

This should give a bareword error if you're using strict, unless there's meaningful code you haven't shown:

print "<br>STATUS = ".$myarray[GeocodeResponse][status]."

I think the other response is correct that you want to use a hash, not an array, but I can't make sense of this code. You're honestly much, much better off using a real XML parser. I know you think that's going to be more work, but it's not. It's really not.

Improve your skills with Modern Perl: the free book.

Replies are listed 'Best First'.
Re^2: Working through it...
by Inexistence (Acolyte) on Sep 11, 2011 at 00:00 UTC

    I guess its because no-one knows what i'm tring to do because my code is so rudimentary. Previously I was using hashes, then was told to instead use arrays, so I did that with limited success. Almost had it working in fact, then it collapsed. I'll go back to hashes because I DO NEED the named indexes. I'll fully read that next recommended section a couple times re hashes

    Do you mean instead $currVar = \@myarray;?

    Using the @ was causing errors for me

    What do you expect this to do? for ($i = 1; $i <= $depth; $i++) { $currVar=\@currVar[$arrayKey[$i]]; }

    This is supposed to make the reference to the variable according to depth, so I can then enter a value. @arrayKey contains all of the variable names, so the loop just inserts them in order, creating the reference

    if($line =~ /^\s{0,4}\<\/(.*)\>/) {

    The data this code will receive has 0-4 spaces depending on the depth of the value. I'm sure there's a better way to do it, but for now it works

    I know you think that's going to be more work, but it's not. It's really not

    If I only use mods, i'd most certainly succeed. But i'm trying to learn HOW to do this at the same time. I've bashed my head into walls numerous times, but the picture is becoming clearer, more and more reading, more attempts, days of failed code attempts. More recommendations on things to read, subtle improvements each time(Note: my server admin is a cow and only allows some mods. My other server only allows other mods, I need a new server)

    As always, thank-you for your input *bow*

      Using the @ was causing errors for me

      Right. Array elements and hash values can only be scalars. What do you expect $currVar[$arrayKey[$i]] to contain?

      But i'm trying to learn HOW to do this at the same time.

      I've been writing Perl 5 professionally since 1998. I probably could write robust XML parsers in multiple languages, but I never do because it's really not worth the time getting things right. I'm sure I'd learn a few things doing it even with my experience, but that's not worth it either, because parsing XML is tricky.

      You're just learning how to use arrays and hashes in Perl. You've already spent several days getting this far; expect to spend at least that long figuring out robust parsing of the XML you've shown. Your reach exceeds your grasp, which is not a bad thing, but you've chosen a particularly pernicious task to learn.

      I don't mean to discourage you from learning. Certainly you have a lot to admire in sticking with this task—but what you'll learn by using a module in this case is very much more valuable right now at your level of Perl understanding than all the work necessary to make a robust XML parser.

      Improve your skills with Modern Perl: the free book.

        Peace and love :)

        Quote "You're just learning how to use arrays and hashes in Perl. You've already spent several days getting this far; expect to spend at least that long figuring out robust parsing of the XML you've shown. Your reach exceeds your grasp, which is not a bad thing, but you've chosen a particularly pernicious task to learn"

        I know i'm hitting my head against a wall... But if I don't, i'll put the books away and start playing WoW again :P I've got some recommended books to read, that aren't beginner, but intermediate. I'll spend days or months till I start hammering out working code. I love coding, or even the attempt. When I eventually manage to hammer out something working, something 'signature me', I smile inside :) I was just hired into an internet company, with hours that will allow me to code a lot and still earn enough to live, plus they're giving me a new laptop :) (Obviously i'm not coding for them though)

        What do you expect $currVar[$arrayKey[$i]] to contain?

        I'm not sure how to explain it in perl terms, but i'll try. I'm trying to self reference the variable to a different depth within itself

        @myarray=(); $currVar=\$myarray; $currVar=\$currVar[TableDepth1name]; $currVar=\$currVar[TableDepth2name];

        Thus, $currVar should equal the value of

        $myarray[TableDepth1name][TableDepth2name]

        I've managed to build it, even Dumper it and it shows up correctly, but the data is always missing