In basic I say Dim var$(50,50), how to make 2 dimensional aray in Perl?

M

max

In basic language I say
Dim var$(50,50)
or
Dim var$(50,50,50)
, how to make 2 or 3 dimensional array in Perl?

Thanks
 
M

max

In basic language I say
perldoc perldsc

I'm not sure that I understand.
In Perl I must use "arrays of arrays" to make multidimensional array?

Is that mean:

In Perl
$array[3][3]

In Basic is
DIM array (3,3)

?
 
B

Brian McCauley

And perllol too!
I'm not sure that I understand.
In Perl I must use "arrays of arrays" to make multidimensional array?

Yes (as in many (most?) languages).
Is that mean:

In Perl
$array[3][3]

In Basic is
DIM array (3,3)

Perl's inbuilt array type is not of fixed dimention. Just use it as you
want. Thanks to a little thing we call autovivifiction simply referring
to $array[2][2] will automagically create an new anonymous array and
store a reference (pointer) to it in $array[2].

If you really want to prepopulate...

my @array = map {[(undef) x 3 ]} 1..3; # 3x3 array of undef

But usually there's no reason to prepopulate...

my @array; # Empty array use it as you want
 
J

Jürgen Exner

max said:
I'm not sure that I understand.
In Perl I must use "arrays of arrays" to make multidimensional array?

Correct. Or rather almost correct because "array of array" is actually a
sloppy term for "array of references to arrays".
Is that mean:

In Perl
$array[3][3]

In Basic is
DIM array (3,3)

No. $array[3][3] is one individual/specific element of a nested array (it
just happens to be the third element in the third array). Your Basic
statement on the other hand declares a multidimensional array with 3x3
elements. Two completely different things.

The closest corresponding Perl code would be
my @array;
but that neither indicates the size (the size of Perl arrays is dynamic) nor
that the values will be references to arrays (Perl doesn't care).

jue
 
T

Tad McClellan

Jürgen Exner said:
No. $array[3][3] is one individual/specific element of a nested array (it
just happens to be the third element in the third array).
^^^^^ ^^^^^

s/third/fourth/g; # :)
 
R

RedGrittyBrick

Abigail said:
my @power;

for (my $x = 0; $x < 3; $x ++) {
for (my $y = 0; $y < 5; $y ++) {
$power [$x] [$y] = $x ** $y;
}
}

I much prefer ...

for my $x (0..2) {
for my $y (0..4) {
$power [$x] [$y] = $x ** $y;
}
}

.... perhaps because I'm not a C programmer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top