Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: What do I do wrong? I want an array of arrays

by sk (Curate)
on Sep 27, 2005 at 18:45 UTC ( [id://495500]=note: print w/replies, xml ) Need Help??


in reply to What do I do wrong? I want an array of arrays

Where am I wrong?

almost every line? :)

Let's deal with this by running the program. first trun on strict and wranings warnings. See my first 2 lines in the code

use strict; use warnings; my @b; my @c={ 22,44,55}; my @d={ "yy","tt"}; $b[0]=@c; # are you trying to create 2-D array? $b[1]=@d; my @e= $b[1]; # i think you want b as 2-D array print "<$e[0]>\n";

when you run it as is you get this warning

Odd number of elements in anonymous hash at nov line 7.<1> Once you change {} to  () the code works but it does not do what you want. See $b[0] is a scalar and an array when evaluated under the scalar context returns the lenght of the array so bascially  $b[0] = @c; will put 3 into $b[0] or the first element of b.

If you want to do 2-D arrays you want to send them as references.

The code below does that

use strict; use warnings; my @b; my @c=( 22,44,55); # changed to () my @d=( "yy","tt"); # changed to () $b[0]=\@c; # \@ creates a reference to the array @c. $b[1]=\@d; my @e= @{$b[1]}; # $b[1] is an array ref so use @{} to get back +the list. print "<$e[0]>\n";

output:

<yy>

hope this helps

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-25 08:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found