2D array of real numbers - Perl

This is a discussion on 2D array of real numbers - Perl ; 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 ...

+ Reply to Thread
Results 1 to 9 of 9

2D array of real numbers

  1. Default 2D array of real numbers

    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!


  2. Default Re: 2D array of real numbers

    jeanluc <jeanluc_picard_66@hotmail.com> 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



  3. Default Re: 2D array of real numbers

    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.

  4. Default Re: 2D array of real numbers

    Works great!

    Thanks!!


  5. Default Re: 2D array of real numbers

    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;

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

    Petr Vileta, Czech republic
    (My server rejects all messages from Yahoo and Hotmail. Send me your mail
    from another non-spammer site please.)




  6. Default Re: 2D array of real numbers

    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

  7. Default Re: 2D array of real numbers

    Petr Vileta <stoupa@practisoft.cz> 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/"

  8. Default Re: 2D array of real numbers

    John W. Krahn wrote:
    > Petr Vileta wrote:
    >>> $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.


    Right ;-)
    my $no_rows = 50;
    my $no_columns = 63;
    my @row=(0.0)x$no_columns;
    my @array;
    foreach (1..$no_rows) { push @array,[@row]; }

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

    >

    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.
    --

    Petr Vileta, Czech republic
    (My server rejects all messages from Yahoo and Hotmail. Send me your mail
    from another non-spammer site please.)



  9. Default Re: 2D array of real numbers

    On 2007-08-30 16:44:57 +0200, "Petr Vileta" <stoupa@practisoft.cz> 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 to Thread

Similar Threads

  1. expression using either real or integer numbers
    By Application Development in forum Fortran
    Replies: 3
    Last Post: 10-07-2007, 04:49 AM
  2. Re: VHDL switch in real numbers
    By Application Development in forum vhdl
    Replies: 1
    Last Post: 10-29-2006, 04:38 PM
  3. Re: VHDL switch in real numbers
    By Application Development in forum vhdl
    Replies: 2
    Last Post: 10-26-2006, 03:00 PM
  4. REAL CREDIT CARD NUMBERS HACK!!
    By Application Development in forum ADO DAO RDO RDS
    Replies: 0
    Last Post: 11-19-2004, 01:23 PM
  5. formats real numbers
    By Application Development in forum basic.visual
    Replies: 4
    Last Post: 07-30-2004, 03:45 AM