For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2005 > Re: Forcing array context









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Forcing array context
John W. Krahn

2005-08-20, 6:56 pm

Binish A R wrote:
> How can I force array context ...


Sorry, you can't. You can have either list context or scalar context or void
context.

perldoc -f wantarray


> like
>
> # echo hello: world | perl -lne '$aref = split(/:/, $_); print $aref'
>
> but thatz giving the length of the array


That is impossible as there is no array there.

perldoc -q "What is the difference between a list and an array"


> ... I want $aref to be a reference ...
> How is that possible ??


You need to copy the list returned from split into an anonymous array and
assign that to the scalar variable.

# echo hello: world | perl -lne '$aref = [ split /:/ ]; print $aref'

perldoc perldata
perldoc perldsc
perldoc perllol



John
--
use Perl;
program
fulfillment
Peter Rabbitson

2005-08-21, 2:55 am

> perldoc -q "What is the difference between a list and an array"

As a side note to the POD above - although lists can not change sizes
they are addressable just like arrays. In other words the following two
statements are equivalent:

my @slice = @{ [ (split /\|/, 'a|b|c|d|e') ] }[1,3];

my @slice = (split /\|/, 'a|b|c|d|e')[1,3];


Peter
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com