Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > 2D array of real numbers

Reply
Thread Tools

2D array of real numbers

 
 
jeanluc
Guest
Posts: n/a
 
      08-29-2007
I want to create a 2D array whose values initially contains 0.0

>From "http://www.xav.com/perl/lib/Pod/perllol.html"


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!

 
Reply With Quote
 
 
 
 
Peter Makholm
Guest
Posts: n/a
 
      08-29-2007
jeanluc <(E-Mail Removed)> writes:

> 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:umper -le '$a = [ (0.0) x 10 ]; print Dumper $a'

//Makholm


 
Reply With Quote
 
 
 
 
Mirco Wahab
Guest
Posts: n/a
 
      08-29-2007
jeanluc wrote:
> 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.
 
Reply With Quote
 
jeanluc
Guest
Posts: n/a
 
      08-29-2007
Works great!

Thanks!!

 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      08-30-2007
Petr Vileta wrote:
> jeanluc wrote:
>> I want to create a 2D array whose values initially contains 0.0
>>
>>> From "http://www.xav.com/perl/lib/Pod/perllol.html"

>>
>> 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
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      08-30-2007
Petr Vileta <(E-Mail Removed)> wrote:

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



No you don't.

Both forms are equivalent.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      08-30-2007
On 2007-08-30 16:44:57 +0200, "Petr Vileta" <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding 1000 largest numbers from a file having some billion numbers Subra C Programming 25 03-08-2007 01:31 AM
Detailsview shows real numbers with commata: Error converting data type nvarchar to real! Curious Trigger ASP .Net 2 09-09-2006 10:59 PM
Frame Numbers vs. JPEG Numbers With CF Cards mort Digital Photography 3 02-16-2005 01:43 AM
Array sort function sorts on chars not numbers ... help ! how to sort numbers GIMME Javascript 5 07-26-2004 01:28 AM
hex array to array of 16 bit numbers? Todd Gardner Python 4 12-27-2003 12:03 AM



Advertisments