Trouble initializing multidimensional array
Trouble initializing multidimensional array
am 17.04.2008 18:30:30 von 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
Re: Trouble initializing multidimensional array
am 17.04.2008 18:56:12 von it_says_BALLS_on_your forehead
On Apr 17, 12:30=A0pm, dave wrote:
> I have trouble =A0initializing =A0the following multidimensional array
>
> my @Table =3D ( [],[],[],[] );
>
> push (@Table [ 't1', 't2', 't3', 't4' ] ) ;
> push (@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 @Table, [ 't1', 't2', 't3', 't4' ];
push @Table, [ 'x1', 'x2', 'x3', 'x4' ];
>
> foreach my $next (@Table)
> {
> =A0 =A0if ( @$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 @Table so that the first entry should have 't1'
> 't2' .. .in it.
>
Re: Trouble initializing multidimensional array
am 17.04.2008 19:00:34 von glex_no-spam
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
"] )"
Re: Trouble initializing multidimensional array
am 17.04.2008 19:27:11 von dave
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
Re: Trouble initializing multidimensional array
am 17.04.2008 19:51:16 von Sherm Pendley
dave writes:
> 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.
What else would you expect? The first thing you added to @Table was refs
to four empty arrays.
my @Table = ( [],[],[],[] );
Those array references aren't going away just because you push more onto
the end of @Table later on - they'll still be there.
> How can in initialize @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 @Table;
sherm--
--
My blog: http://shermspace.blogspot.com
Cocoa programming in Perl: http://camelbones.sourceforge.net
Re: Trouble initializing multidimensional array
am 17.04.2008 20:18:56 von glex_no-spam
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.
>
> }
> }
Re: Trouble initializing multidimensional array
am 17.04.2008 20:32:41 von Keith Keller
On 2008-04-17, dave 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@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