Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Just a quick question!

Is there a shorthand way to assign multiple array elements (from the same array) to new variables? Ordinarily I would use:

my ($var1, $var2, $var3, $var4) = ($ar[0],$ar[1],$ar[4],$ar[7])

However it would be nicer if this could be done where the array handle only has to be specified once e.g:

$ar[0,1,4,7]

(which does not appear to populate the variables)

Replies are listed 'Best First'.
Re: Assign multiple array elements to new variables
by hippo (Archbishop) on Sep 05, 2016 at 12:34 UTC
    #!/usr/bin/env perl use strict; use warnings; my @ar = qw/Sun Mon Tue Wed Thu Fri Sat Sun/; my ($var1, $var2, $var3, $var4) = @ar[0,1,4,7]; print "$var1, $var2, $var3, $var4\n";

    Array slices (or sections) use the @ symbol.

Re: Assign multiple array elements to new variables
by AnomalousMonk (Archbishop) on Sep 05, 2016 at 13:30 UTC