[Melbourne-pm] Referencing an item in a list

Paul Fenwick pjf at perltraining.com.au
Sun Mar 26 20:19:14 PST 2006


G'day Brad,

Bradley Dean wrote:

> How do I directly reference a member of a returned array - ie (in broken
> code):
> 
> sub gimme_an_array { return (1,2,3,4,5,6); }
> my $third_member = gimme_an_array()[2];

Try:

	my $third_member = (gimme_an_array())[2];

The extra set of parens tells Perl "here comes a list", which you may then index
at your pleasure.

Cheerio,

	Paul

-- 
Paul Fenwick <pjf at perltraining.com.au> | http://perltraining.com.au/
Director of Training                   | Ph:  +61 3 9354 6001
Perl Training Australia                | Fax: +61 3 9354 2681


More information about the Melbourne-pm mailing list