About 2D array?

Discussion in 'Perl Misc' started by Davy, Jul 16, 2006.

  1. Davy

    Davy Guest

    Hi,

    I want to use 2D array in Perl.
    For example, @a = (1,2,3); @b = (1,2,4,5);
    When I use @c = (@a,@b); @c is stored as a 1D array.

    But I want a array can be accessed by $c[j],
    Can I do 2D array in Perl?

    Thanks!

    Davy
    Davy, Jul 16, 2006
    #1
    1. Advertising

  2. "Davy" <> writes:

    > Hi,
    >
    > I want to use 2D array in Perl.
    > For example, @a = (1,2,3); @b = (1,2,4,5);
    > When I use @c = (@a,@b); @c is stored as a 1D array.
    >
    > But I want a array can be accessed by $c[j],
    > Can I do 2D array in Perl?


    Keep references to arrays in @a.

    @a = (1,2,3); @b = (1,2,4,5);
    @c = ([@a],[@b]);
    print $c[1][3];

    --
    [pl2en: Andrew] Andrzej Adam Filip : :
    http://anfi.homeunix.net/
    Andrzej Adam Filip, Jul 16, 2006
    #2
    1. Advertising

  3. Davy

    Paul Lalli Guest

    Davy wrote:
    > I want to use 2D array in Perl.


    Technically, Perl doesn't have two-dimensional arrays. What it does
    have is arrays of array references, which function much like 2d arrays
    in other languages.

    > For example, @a = (1,2,3); @b = (1,2,4,5);
    > When I use @c = (@a,@b); @c is stored as a 1D array.


    Yes it is, just as the documentation (perldoc perldata) says will
    happen.

    > But I want a array can be accessed by $c[j],
    > Can I do 2D array in Perl?


    You can simulate it, using array references. Please read:
    perldoc perlreftut
    perldoc perllol

    Paul Lalli
    Paul Lalli, Jul 16, 2006
    #3
    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. Daniel
    Replies:
    1
    Views:
    1,050
    Kevin Collins
    Aug 14, 2003
  2. S300
    Replies:
    4
    Views:
    21,993
    Roedy Green
    Aug 19, 2003
  3. Mara Guida

    const and array of array (of array ...)

    Mara Guida, Sep 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    488
    David RF
    Sep 3, 2009
  4. Tom
    Replies:
    3
    Views:
    208
    salsablr
    Dec 20, 2004
  5. Tuan  Bui
    Replies:
    14
    Views:
    473
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page