Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Initialize multiple variables in one statement

by Anonymous Monk
on Nov 23, 2015 at 18:56 UTC ( [id://1148417]=perlquestion: print w/replies, xml ) Need Help??

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

Hi there Monks!

Is here a better way to initialize multiple variables in one statement other than this?
... my ($name1, $name2, $name3, $name4, $name5, $name6); $name1 = $name2 = $name3 = $name4 = $name5 = $name6 = ''; print "\n $name1, $name2, $name3, $name4, $name5, $name6\n\n"; ...

Thanks!

Replies are listed 'Best First'.
Re: Initialize multiple variables in one statement
by stevieb (Canon) on Nov 23, 2015 at 19:05 UTC

    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

      I think the solution might be closer to:

      use strict; use warnings; my ($name1, $name2, $name3, $name4, $name5, $name6) = ('') x 6; print "\n $name1, $name2, $name3, $name4, $name5, $name6\n\n";

      Using '' x 6 in scalar context just concats the empty string six times yielding just an empty string. So:

      use strict; use warnings; my @names = '' x 6; print "$names[2]\n"; __END__ Use of uninitialized value $names[2] in concatenation (.) or string at + ...
      Ron

        Nice catch! :)

Re: Initialize multiple variables in one statement
by GrandFather (Saint) on Nov 23, 2015 at 23:51 UTC

    Why would you want to? As hinted by stevieb "numbered" variable are almost always the wrong way to do it. Tell us more about what you are doing and we can give a much better answer that will continue to work when you need to add or remove a few names and will be much easier to understand (once you understand arrays or hashes).

    Premature optimization is the root of all job security
Re: Initialize multiple variables in one statement
by choroba (Cardinal) on Nov 23, 2015 at 22:18 UTC
    TIMTOWTDI, I've seen this with 2 or 3 variables:
    my $name1 = my $name2 = my $name3 = my $name4 = my $name5 = my $name6 += '';
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Initialize multiple variables in one statement
by AnomalousMonk (Archbishop) on Nov 23, 2015 at 21:30 UTC

    More of a gee-whiz than anything else, but at least the following allows lexicals to be added and removed without worrying about adjusting any initializer list or repetition count:

    c:\@Work\Perl\monks>perl -wMstrict -le "$_ = 'phil' for my ($name1, $name2, $name3, $name4); print qq{'$name1' '$name2' '$name3' '$name4'}; ;; my $init = 42; $_ = $init++ for my ($n1, $n2, $n3); print qq{$n1, $n2, $n3}; " 'phil' 'phil' 'phil' 'phil' 42, 43, 44


    Give a man a fish:  <%-{-{-{-<

Re: Initialize multiple variables in one statement
by shadowsong (Pilgrim) on Nov 23, 2015 at 20:08 UTC

    Hello Anony-monk,

    Any reason you couldn't just do this:

    my ($name1,$name2,$name3,$name4,$name5,$name6) = ('','','','','','');

    Cheers,
    Shadowsong

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1148417]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (7)
As of 2024-03-28 22:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found