CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [skysurf] iterating through an array using XML::Simple: Edit Log



Laurent_R
Enthusiast / Moderator

Oct 23, 2012, 10:55 AM


Views: 984
Re: [skysurf] iterating through an array using XML::Simple

A short test under de Perl debugger:



Code
  DB<8> @c = qw /foo bar baz/ 

DB<9> x @c
0 'foo'
1 'bar'
2 'baz'
DB<10> $i = 0

DB<11> print $c['$i']
foo
DB<12> $i = 1

DB<13> print $c['$i']
foo
DB<14> $i = 2

DB<15> print $c['$i']
foo
DB<16> print $c[$i]
baz


As you can see, when I use '$i' as index of the array, I get only the first element of the array, irrespective of the value of $i, as if '$i' was evaluated to 0 in this context (which happens when you evaluate a string in a numeric context). You really need to remove the single quote marks.

With double quote marks, it would work:




Code
  DB<17> print $c["$i"] 
baz


I think that your use of an array slice is also probably an error. You should probably prefix your array name with $ instead of @ (on the line where you are using the $i subscript).


(This post was edited by Laurent_R on Oct 24, 2012, 2:21 AM)


Edit Log:
Post edited by Laurent_R (Enthusiast) on Oct 23, 2012, 10:58 AM
Post edited by Laurent_R (Enthusiast) on Oct 24, 2012, 2:21 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives