Trouble initializing multidimensional array

I have trouble initializing the following multidimensional array

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

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

foreach my $next ( [at] Table)
{
if ( [at] $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 [at] Table so that the first entry should have 't1'
't2' .. .in it.

Thanks

David
dave [ Do, 17 April 2008 18:30 ] [ ID #1944863 ]

Re: Trouble initializing multidimensional array

On Apr 17, 12:30=A0pm, dave <davewu... [at] gmail.com> wrote:
> I have trouble =A0initializing =A0the following multidimensional array
>
> my [at] Table =3D ( [],[],[],[] );
>
> push ( [at] Table [ 't1', 't2', 't3', 't4' ] ) ;
> push ( [at] Table [ 'x1', 'x2', 'x3', 'x4' ] ); =A0# and so on

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

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



>
> foreach my $next ( [at] Table)
> {
> =A0 =A0if ( [at] $next[0] eq 'xx' )

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

> =A0 =A0{
> =A0 =A0 =A0 =A0 # do something
> =A0 =A0}
>
> }
>
> When I run the foreach loop, the first 4 times will have "Use of
> uninitialized value" =A0error and then everything will be fine.
> How can in initialize [at] Table so that the first entry should have 't1'
> 't2' .. .in it.
>
it_says_BALLS_on_your [ Do, 17 April 2008 18:56 ] [ ID #1944864 ]

Re: Trouble initializing multidimensional array

dave wrote:
> I have trouble initializing the following multidimensional array
>
> my [at] Table = ( [],[],[],[] );
No need to do that.

my [at] Table;

If you had:

use strict;
use warnings;

You'd see errors with your syntax for push:

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

perldoc -f push

" push ARRAY,LIST "


>
> foreach my $next ( [at] Table)
> {
> if ( [at] $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 [at] 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
"] )"
glex_no-spam [ Do, 17 April 2008 19:00 ] [ ID #1944865 ]

Re: Trouble initializing multidimensional array

On Apr 17, 1:00 pm, "J. Gleixner" <glex_no-s... [at] qwest-spam-no.invalid>
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 [at] Table;

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

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

}
}


Thanks again.
David
dave [ Do, 17 April 2008 19:27 ] [ ID #1944866 ]

Re: Trouble initializing multidimensional array

dave <davewu922 [at] gmail.com> writes:

> I have trouble initializing the following multidimensional array
>
> my [at] Table = ( [],[],[],[] );
>
> push ( [at] Table [ 't1', 't2', 't3', 't4' ] ) ;
> push ( [at] Table [ 'x1', 'x2', 'x3', 'x4' ] ); # and so on
>
> foreach my $next ( [at] Table)
> {
> if ( [at] $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.

What else would you expect? The first thing you added to [at] Table was refs
to four empty arrays.

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

Those array references aren't going away just because you push more onto
the end of [at] Table later on - they'll still be there.

> How can in initialize [at] Table so that the first entry should have 't1'
> 't2' .. .in it.

If you don't want four empty arrays to begin with, don't add them:

my [at] Table;

sherm--

--
My blog: http://shermspace.blogspot.com
Cocoa programming in Perl: http://camelbones.sourceforge.net
Sherm Pendley [ Do, 17 April 2008 19:51 ] [ ID #1944868 ]

Re: Trouble initializing multidimensional array

dave wrote:
> On Apr 17, 1:00 pm, "J. Gleixner" <glex_no-s... [at] qwest-spam-no.invalid>
> 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 [at] Table;
>
> push ( [at] Table, [ 't1', 't2', 't3', 't4' ] ) ;
> push ( [at] Table, [ 'x1', 'x2', 'x3', 'x4' ] ); # and so on
>
> foreach my $next ( [at] Table)
> {
> if ( [at] $next->[0] eq 't1' )

Why do you keep wanting to put a ' [at] ' there?

Maybe this will explain:

perldoc -q 'What is the difference between'

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

See, no ' [at] ' is needed/wanted.

>
> }
> }
glex_no-spam [ Do, 17 April 2008 20:18 ] [ ID #1944869 ]

Re: Trouble initializing multidimensional array

On 2008-04-17, dave <davewu922 [at] gmail.com> wrote:
>
> The push statement was a typo, it had a comma in the actual code.

Don't retype code; cut and paste, so that typos don't confuse the issue.

--keith



--
kkeller-usenet [at] wombat.san-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
see X- headers for PGP signature information
Keith Keller [ Do, 17 April 2008 20:32 ] [ ID #1944871 ]
Perl » comp.lang.perl.misc » Trouble initializing multidimensional array

Vorheriges Thema: FAQ 9.19 How do I return the user's mail address?
Nächstes Thema: FAQ 9.20 How do I send mail?