Mike Liss wrote:
> 
> Hello,

Hello,

> I would like to know if it is possible to do somthing like this:
> 
> ( I know I can't do it this way as Perl barfs when I tried it )
> 
> 
> $SizeOfMyArray = 2;
> 
> $MyArray[ $SizeOfMyArray ];

If you want to pre-allocate the array size the proper way to do it is:

$#MyArray = 1;  # pre-allocate a two element array (elements 0 and 1)

Although you usually don't need to as perl will grow the array as
needed.


> push( @MyArray[ 0 ],  0 );         << --- complains here with < ERROR > (see
> below )
> push( @MyArray[ 1 ],  5 );

push (and pop and shift and unshift and splice) only work on arrays not
slices or lists.  You probably want:

$MyArray[ 0 ] = 0;  # assign literals to scalars
$MyArray[ 1 ] = 5;

Or simply:

@MyArray[ 0, 1 ] = ( 0, 5 );  # assign list to array slice

Or if you must use push:

push @MyArray, 0, 5;  # add two elements to the end of the array

Or another way to do the second example above:

splice @MyArray, 0, 2, ( 0, 5 );




John
-- 
use Perl;
program
fulfillment

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

Reply via email to