2D array of real numbers

J

jeanluc

I want to create a 2D array whose values initially contains 0.0

I see that the following code will set up a 3x2 2D array:

@2D_array = (
[0.0, 0.0, 0.0],
[0.0, 0.0, 0.0],
);

The above works fine but unfortunately I might have to make some large
arrays of arbitrary size. Doing it manually like above is not
possible.

I want to use the variables

$no_rows = 50;
$no_columns = 63;

To define a 50x63 2D arbitrary array filled with 0.0.

Anybody know how to do this?

Thanks!
 
P

Peter Makholm

jeanluc said:
I want to use the variables

$no_rows = 50;
$no_columns = 63;

To define a 50x63 2D arbitrary array filled with 0.0.

Anybody know how to do this

The x operator would be usefull for this.

perl -MData::Dumper -le '$a = [ (0.0) x 10 ]; print Dumper $a'

//Makholm
 
M

Mirco Wahab

jeanluc said:
I want to create a 2D array whose values initially contains 0.0
To define a 50x63 2D arbitrary array filled with 0.0.

Anybody know how to do this?

...
use constant NROW => 50; # set number of rows
use constant NCOL => 63; # set number of columns

my @Arr2D =
map [ ( 0.0 ) x NCOL ], # generate single ROW of NCOL COLUMNS
1 .. NROW; # NROW ROWS


print map "@$_\n", @Arr2D;
...

Regards

M.
 
J

John W. Krahn

Petr said:
jeanluc said:
I want to create a 2D array whose values initially contains 0.0

I see that the following code will set up a 3x2 2D array:

@2D_array = (
[0.0, 0.0, 0.0],
[0.0, 0.0, 0.0],
);

The above works fine but unfortunately I might have to make some large
arrays of arbitrary size. Doing it manually like above is not
possible.

I want to use the variables

$no_rows = 50;
$no_columns = 63;

my $no_rows = 50;
my $no_columns = 63;
my @row=(0.0)x$no_columns;
my @array=([@row])x$no_rows;

You are making $no_rows copies of the *same* anonymous array so any change to
$array[0] will also show up in $array[1] and $array[2] and $array[3] and etc.

But you must address element as $array[row]->[col] instead of
$array[row][col].

Because you said so? I don't think so.



John
 
A

Anno Siegel

On 2007-08-30 16:44:57 +0200, "Petr Vileta" <[email protected]> said:

[ $array[ $i]->[ $k] vs. $array[ $i][ $k] ]
Right too. I remember that in some case $array[row][col] generate error
at runtime, some like "$array[row][col] is not allowed while use strict
refs", but I forgot details and context.

No, there is no such runtime error. An arrow (->) that appears in the
middle of a pair of
closing and opening parentheses (of any kind) can be dropped without a
change in meaning.

Anno
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top