|
Prev: Trouble initializing multidimensional array
Next: FAQ 9.19 How do I return the user's mail address?
From: nolo contendere on 17 Apr 2008 12:56 On Apr 17, 12:30 pm, dave <davewu...(a)gmail.com> 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. >
From: dave on 17 Apr 2008 13:27 On Apr 17, 1:00 pm, "J. Gleixner" <glex_no-s...(a)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 @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
From: J. Gleixner on 17 Apr 2008 14:18 dave wrote: > On Apr 17, 1:00 pm, "J. Gleixner" <glex_no-s...(a)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 @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. > > } > }
From: Keith Keller on 17 Apr 2008 14:32 On 2008-04-17, dave <davewu922(a)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(a)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
|
Pages: 1 Prev: Trouble initializing multidimensional array Next: FAQ 9.19 How do I return the user's mail address? |