简体   繁体   中英

Perl - cast hash to array without using further variable

is possible to cast hash to array in perl but without using further variable ? This works as expected but another variable (@arr) is used:

perl -wlae '%hash=(name=>"linus", forename=>"torvalds "); @arr=%hash; print "@arr"'

I have tried those but neither worked (BTW what do they do ?):

perl -wlae '%hash=(name=>"linus", forename=>"torvalds "); print "@hash"'
perl -wlae '%hash=(name=>"linus", forename=>"torvalds "); print "%@hash"'
perl -wlae '%hash=(name=>"linus", forename=>"torvalds "); print "@%hash"'
perl -wlae '%hash=(name=>"linus", forename=>"torvalds "); print (%hash);'

Putting an expression in parentheses evaluates it in a list context, just like assigning to a list variable does.

In the case of print , this casting is unnecessary, since it uses list context for both arrays and hashes. But if you want to do other array-style things with a hash, you can use this, eg

$first = (%hash)[0];
echo $first;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM