Trouble initializing multidimensional array

D

dave

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
 
N

nolo contendere

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

J. Gleixner

dave said:
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
"] )"
 
D

dave

On Apr 17, 1:00 pm, "J. Gleixner" <[email protected]>
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
 
J

J. Gleixner

dave said:
On Apr 17, 1:00 pm, "J. Gleixner" <[email protected]>
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top