in reply to Initialize multiple variables in one statement
You can use a hash and populate it based on a list with map:
my %names = map { $_ => '' } qw(foo bar baz); print "$names{$_}\n" for keys %names; # access individually print "$names{foo}\n";
You can also use an array, but it'll only work if your variables are truly in a numbered order. Note that an array starts at zero and not one though. If your vars aren't literally ordered, use a hash instead.
my @names = ('') x 6; # the following is wrong, pointed out in the reply by mr_ron # my @names = '' x 6; print "$_\n" for @names; # access individually print "$names[2]\n"; # prints 3rd element
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Initialize multiple variables in one statement
by mr_ron (Deacon) on Nov 23, 2015 at 20:13 UTC | |
by stevieb (Canon) on Nov 23, 2015 at 20:29 UTC |