2D array of real numbers

Discussion in 'Perl Misc' started by jeanluc, Aug 29, 2007.

  1. jeanluc

    jeanluc Guest

    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!
    jeanluc, Aug 29, 2007
    #1
    1. Advertising

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

    //Makholm
    Peter Makholm, Aug 29, 2007
    #2
    1. Advertising

  3. jeanluc

    Mirco Wahab Guest

    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.
    Mirco Wahab, Aug 29, 2007
    #3
  4. jeanluc

    jeanluc Guest

    Works great!

    Thanks!!
    jeanluc, Aug 29, 2007
    #4
  5. 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
    John W. Krahn, Aug 30, 2007
    #5
  6. Petr Vileta <> 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/"
    Tad McClellan, Aug 30, 2007
    #6
  7. jeanluc

    Anno Siegel Guest

    On 2007-08-30 16:44:57 +0200, "Petr Vileta" <> 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
    Anno Siegel, Aug 30, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeremy Pyle

    Re: Representation of real numbers

    Jeremy Pyle, Jun 27, 2003, in forum: VHDL
    Replies:
    1
    Views:
    1,261
    Ken Smith
    Jun 27, 2003
  2. Jim Lewis

    Re: Representation of real numbers

    Jim Lewis, Jul 1, 2003, in forum: VHDL
    Replies:
    0
    Views:
    1,807
    Jim Lewis
    Jul 1, 2003
  3. senthil
    Replies:
    5
    Views:
    1,367
    senthil
    Jan 24, 2004
  4. Curious Trigger
    Replies:
    2
    Views:
    1,800
    Curious Trigger
    Sep 9, 2006
  5. GIMME
    Replies:
    5
    Views:
    178
    Thomas 'PointedEars' Lahn
    Jul 26, 2004
Loading...

Share This Page