Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I turn an array returned from a function (like split) into an array reference?

Consider this code:

@tmp = split(/\s+/, "apple banana cherry");
$aref = \@tmp;

Besides being inelegant, the above code is fragile. Say I follow it with this line:

@tmp = split(/\s+/, "dumpling eclair fudge");

Now $$aref[1] is "eclair" instead of "banana".

How can I avoid the use of the temp variable?

Conceptually, I'm thinking of something like

$aref = \@{split(/\s+/, "apple banana cherry")};
like image 548
dreeves Avatar asked Nov 07 '11 04:11

dreeves


1 Answers

You could do this if you want an array-ref:

my $aref = [ split(/\s+/, "apple banana cherry") ];
like image 146
mu is too short Avatar answered Oct 20 '22 00:10

mu is too short