Jim Gibson wrote:
At 2:00 AM -0700 9/25/10, feltra wrote:

Am using arrays with only references in a sub-routine. While I got
the hang of how to access an element of the array using the '->'
operator, I do not know how to intialize this array. I.e. I want to
be able to do something like

@myarr=(); $#myarr = -1;

inside the subroutine, but myarr is only a reference to an array not
the actual array...

[] is the notation for a reference to an array.

\@ is the notation for a reference to an array, [] is an anonymous array.


For example, if you want to initialize a scalar variable so that it
contains a reference to an anonymous, empty array, you would do this:

my $myarr = [];

You can initialize the array to contain data:

my $myarr = [ 'a', 'b', 'c' ];

or

my $myarr = [ qw( a b c) ];

To copy the contents of an array into an anonymous array and save a
reference to the anonymous array:

my $myarr = [ @other_array ];

Does that help?

That is not relevant to the OP's question.



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to