Thank you. That is all find and dandy now. But what about the print line:

I am trying to print the newest value in the array.

$questionpos[$questionno][$#{$questionpos[$questionno]}]

-----Original Message-----
From: Peter Scott [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, June 05, 2002 2:02 PM
To: Balint, Jess
Cc: begin
Subject: Re: Multi-Dimensional Array and push()


At 01:19 PM 6/5/02 -0400, you wrote:
>Hello all. I have trying to push a value onto the end of a two-dimension
>array. Here is my code.
>
>                         if( /\s+--\s+COLS\.\s+(\d+)\s+-\s+(\d+)\s+--/ ) {
>                                 push( @{questionpos[$questionno]}, $1 );

Nope, Perl thinks you're trying to make an array slice.  Close though.

>                                 push @questionlength[$questionno], ( $2 -
$1
>+ 1 );
>                         } elsif( /\s+--COL\.\s+(\d+)\s+--/ ) {
>                                 push @questionpos[$questionno], $1;
>                                 push @questionlength[$questionno], 1;

Nope, Perl thinks that's an array slice with one element in it (and hence 
warns).

>                         }
>                         print "Match column definition
>\@$questionpos[$questionno][$#questionpos[$questionno]]\n" if( $d == 1 );
>
>I tried a couple different things, but it wouldn't work right. How do I
push
>these values? Thanks.

Two rules to remember for dereferencing:

(1) You can replace the identifier portion (the "foo" in "@foo") of any 
expression with a simple scalar ($foo) that is a reference to the 
appropriate thing.
(2) You can replace the identifier with a block evaluating to a reference 
to the appropriate thing.

(Well, there's also the arrow operator, but that doesn't concern us here.)

You would be doing "push (@foo, $value)" if you were pushing onto an 
ordinary array @foo.  But you don't have an ordinary array; you have a 
reference to it which is stored in $questionpos[$questionno].  That's not a 
simple scalar, therefore you have to exercise rule 2 and hence:

         push ( @{$questionpos[$questionno]}, $value )


--
Peter Scott
Pacific Systems Design Technologies        Boston Perl Classes in July:
http://www.perldebugged.com/               http://stemsystems.com/class/


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to