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

Discussion in 'Perl Misc' started by max, Jul 9, 2005.

  1. max

    max Guest

    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
    max, Jul 9, 2005
    #1
    1. Advertising

  2. A. Sinan Unur, Jul 9, 2005
    #2
    1. Advertising

  3. max

    max Guest

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

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

    ?
    max, Jul 9, 2005
    #3
  4. Re: In basic I say Dim var$(50,50), how to make 2 dimensional arayin Perl?

    max wrote:

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

    >>
    >>perldoc perldsc

    >


    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
    Brian McCauley, Jul 9, 2005
    #4
  5. max wrote:
    >>> 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?

    >>
    >> perldoc perldsc

    >
    > 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
    Jürgen Exner, Jul 9, 2005
    #5
  6. Jürgen Exner <> wrote:

    > 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; # :)


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jul 9, 2005
    #6
  7. Re: In basic I say Dim var$(50,50), how to make 2 dimensional arayin Perl?

    Abigail wrote:
    > 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.
    RedGrittyBrick, Jul 14, 2005
    #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. ss
    Replies:
    1
    Views:
    1,381
    Gordon Beaton
    Jun 22, 2006
  2. Tom St Denis

    Sorting a string aray in order of string length

    Tom St Denis, Jul 9, 2003, in forum: C Programming
    Replies:
    9
    Views:
    476
    Kevin Easton
    Jul 10, 2003
  3. Denis Palas
    Replies:
    1
    Views:
    501
  4. Florian Kaufmann
    Replies:
    1
    Views:
    377
  5. Joza
    Replies:
    13
    Views:
    183
    -berlin.de
    Oct 3, 2006
Loading...

Share This Page