Trouble initializing multidimensional array

Discussion in 'Perl Misc' started by dave, Apr 17, 2008.

  1. dave

    dave Guest

    I have trouble initializing the following multidimensional array

    my @Table = ( [],[],[],[] );

    push (@Table [ 't1', 't2', 't3', 't4' ] ) ;
    push (@Table [ 'x1', 'x2', 'x3', 'x4' ] ); # and so on

    foreach my $next (@Table)
    {
    if ( @$next[0] eq 'xx' )
    {
    # do something
    }
    }

    When I run the foreach loop, the first 4 times will have "Use of
    uninitialized value" error and then everything will be fine.
    How can in initialize @Table so that the first entry should have 't1'
    't2' .. .in it.

    Thanks

    David
    dave, Apr 17, 2008
    #1
    1. Advertising

  2. Re: Trouble initializing multidimensional array

    On Apr 17, 12:30 pm, dave <> wrote:
    > I have trouble  initializing  the following multidimensional array
    >
    > my @Table = ( [],[],[],[] );
    >
    > push (@Table [ 't1', 't2', 't3', 't4' ] ) ;
    > push (@Table [ 'x1', 'x2', 'x3', 'x4' ] );  # and so on


    you forgot the comma between the array and the value you want to push
    onto the array.

    push @Table, [ 't1', 't2', 't3', 't4' ];
    push @Table, [ 'x1', 'x2', 'x3', 'x4' ];



    >
    > foreach my $next (@Table)
    > {
    >    if ( @$next[0] eq 'xx' )


    $next is an array reference. i think you mean:
    if ( $next->[0] eq 'xx' )

    >    {
    >         # do something
    >    }
    >
    > }
    >
    > When I run the foreach loop, the first 4 times will have "Use of
    > uninitialized value"  error and then everything will be fine.
    > How can in initialize @Table so that the first entry should have 't1'
    > 't2' .. .in it.
    >
    nolo contendere, Apr 17, 2008
    #2
    1. Advertising

  3. dave

    J. Gleixner Guest

    dave wrote:
    > I have trouble initializing the following multidimensional array
    >
    > my @Table = ( [],[],[],[] );

    No need to do that.

    my @Table;

    If you had:

    use strict;
    use warnings;

    You'd see errors with your syntax for push:

    >
    > push (@Table [ 't1', 't2', 't3', 't4' ] ) ;
    > push (@Table [ 'x1', 'x2', 'x3', 'x4' ] ); # and so on


    perldoc -f push

    " push ARRAY,LIST "


    >
    > foreach my $next (@Table)
    > {
    > if ( @$next[0] eq 'xx' )


    That's not how you access the data.

    $next->[0]

    perldoc perlreftut

    > {
    > # do something
    > }
    > }
    >
    > When I run the foreach loop, the first 4 times will have "Use of
    > uninitialized value" error and then everything will be fine.
    > How can in initialize @Table so that the first entry should have 't1'
    > 't2' .. .in it.


    Really? Running your code I get:

    Type of arg 1 to push must be array (not array slice) at - line 2, near
    "] ) "
    Type of arg 1 to push must be array (not array slice) at - line 3, near
    "] )"
    J. Gleixner, Apr 17, 2008
    #3
  4. dave

    dave Guest

    Re: Trouble initializing multidimensional array

    On Apr 17, 1:00 pm, "J. Gleixner" <>
    wrote:

    Thanks ...

    The push statement was a typo, it had a comma in the actual code.

    I changed the code to something like the following and everything
    seems to run.

    my @Table;

    push (@Table, [ 't1', 't2', 't3', 't4' ] ) ;
    push (@Table, [ 'x1', 'x2', 'x3', 'x4' ] ); # and so on

    foreach my $next (@Table)
    {
    if ( @$next->[0] eq 't1' )
    {
    my $col_1 = $next->[1];

    }
    }


    Thanks again.
    David
    dave, Apr 17, 2008
    #4
  5. dave

    J. Gleixner Guest

    Re: Trouble initializing multidimensional array

    dave wrote:
    > On Apr 17, 1:00 pm, "J. Gleixner" <>
    > wrote:
    >
    > Thanks ...
    >
    > The push statement was a typo, it had a comma in the actual code.
    >
    > I changed the code to something like the following and everything
    > seems to run.
    >
    > my @Table;
    >
    > push (@Table, [ 't1', 't2', 't3', 't4' ] ) ;
    > push (@Table, [ 'x1', 'x2', 'x3', 'x4' ] ); # and so on
    >
    > foreach my $next (@Table)
    > {
    > if ( @$next->[0] eq 't1' )


    Why do you keep wanting to put a '@' there?

    Maybe this will explain:

    perldoc -q 'What is the difference between'

    > {
    > my $col_1 = $next->[1];


    See, no '@' is needed/wanted.

    >
    > }
    > }
    J. Gleixner, Apr 17, 2008
    #5
    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. Dave Bazell

    slice of multidimensional array

    Dave Bazell, Jul 23, 2003, in forum: Perl
    Replies:
    2
    Views:
    4,066
  2. epigram
    Replies:
    1
    Views:
    10,780
    =?Utf-8?B?c29jaWV0b3BpYS5uZXQ=?=
    Jul 16, 2005
  3. Ben
    Replies:
    8
    Views:
    12,565
    Eki Y. Baskoro
    Dec 18, 2003
  4. Allan A.
    Replies:
    3
    Views:
    642
    mlimber
    Feb 22, 2006
  5. Van Jacques
    Replies:
    10
    Views:
    191
    Van Jacques
    Dec 6, 2003
Loading...

Share This Page