Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: 2 dimensional array question

by reasonablekeith (Deacon)
on Sep 05, 2005 at 13:58 UTC ( [id://489226]=note: print w/replies, xml ) Need Help??


in reply to 2 dimensional array question

you need square brackets...
$arr[0] = [1,2,3]; $arr[1] = [4,5,6]; $index=1; print "$arr[$index][2]";
The square bracket returns a reference to an anonymous array. Your example is just a list, which is forced to scalar context (you're putting it into the first element of your array) which just returns the last element in your list. You can prove this by printing $arr[0];

You might also have noticed I changed the @'s to $'s, which it prefered when referencing a single element of an array...

my @array = (1,2,3); my $array[0] = 4;
If you run your program with warnings on, it will tell you this.

references are a big subject, you might want to check out ...

perldoc perlref
or "Learing/Programming Perl" which are excellent books.
---
my name's not Keith, and I'm not reasonable.

Replies are listed 'Best First'.
Re^2: 2 dimensional array question
by redss (Monk) on Sep 05, 2005 at 14:50 UTC
    ah yes, the square brackets did the trick. thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://489226]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-19 12:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found