Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Assign multiple scalars the same value

by mhearse (Hermit)
on Jan 24, 2005 at 21:55 UTC ( #424708=perlquestion: print w/ replies, xml ) Need Help??
mhearse has asked for the wisdom of the Perl Monks concerning the following question:

I've noticed that
my ($a,$b,$c) = "TRUE";
doesn't work. Is
for ($a,$b,$c) { $_ = "TRUE" }
the best way to assign the same value to multiple scalars? Can it be done strictly in one swipe? Thanks much.

Comment on Assign multiple scalars the same value
Select or Download Code
Re: Assign multiple scalars the same value
by jdporter (Canon) on Jan 24, 2005 at 21:57 UTC
    my $a = my $b = my $c = "TRUE";
    or perhaps more clearly,
    my $a = my $b = my $c = "TRUE";
    The my operator binds fairly tightly.
Re: Assign multiple scalars the same value
by Tanktalus (Canon) on Jan 24, 2005 at 21:58 UTC
    my ($a, $b, $c) = ('TRUE') x 3;

    Always many ways of doing it ;-)

      Jeepers! the 'x' operator works on lists too!?

      Looks like time to refactor out all ...

      my ($a,$b,$c) = map {'TRUE'}(0 .. 2);

        Yep, it does. Note the difference between these, though:

        $,= " "; $\ = "\n"; print( map { int rand 10 } 0 .. 6 ); print( ( int rand 10 ) x 7 );

        The x operator only evaluates the list expression once. So you can't always substitute one for the other.

        Makeshifts last the longest.

        scooterm,
        Not only that, but if the what the list contains is a reference, the reference is replicated as well. The first useful application I remember seeing of this is in tye's Algorithm::Loops. I found a useful variation that looked a little something like:
        my @signal = qw(ABRT STOP TERM INT); @SIG{ @signal } = (sub { kill 9, $pid; exit }) x @signal;
        Incidently, diotalevi makes a good point concerning ensuring that you have the same number of things on both sides of the assignment which is not an issue in my example.

        Cheers - L~R

      This solution is less good than simply assigning the value to each variable separately in my $a = my $b = my $c = 'TRUE' because it avoids having to count how many variables you have. In your code, if you got the number wrong then the trailing variables will be left undefined which is far and away from the intent to leave them with a true value. You're making it easy to write in a bug later.

        Good point. One could always do this:

        use Want 'howmany'; my ($a, $b, $c) = &{ sub{ (1) x howmany } };

        Not sure if that's shorter/better than other solutions (plus requires Want), but it's another way of keeping the number of variables in sync.

        -xdg

        Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: Assign multiple scalars the same value
by Aristotle (Chancellor) on Jan 24, 2005 at 22:17 UTC

    For comleteness' sake, you can also do the following:

    $_ = "TRUE" for my ( $a, $b, $c );

    That my declaration is actually not restricted to the loop, the variables are available down the block this statement appears in. That will probably surprise most readers of your code, though, so you don't actually want to use it in practice.

    Makeshifts last the longest.

      That hurts my head just to look at! I hope the retribution of the gods for writing it was swift and just! *grin*

      --
      Do not seek to follow in the footsteps of the wise. Seek what they sought. -Basho

Re: Assign multiple scalars the same value
by davido (Archbishop) on Jan 25, 2005 at 05:48 UTC

    Here's another variation on a theme:

    map{ $_ = 'TRUE' } my( $a, $b, $c );

    Not sure if that's taking us farther down an obfuscated road, but it does wield map in void context for the fun of it. ;)


    Dave

Re: Assign multiple scalars the same value
by gube (Parson) on Jan 25, 2005 at 06:16 UTC

    Hi, Just try this it's easy method

    $a = $b = $c = "True"; print "\n$a$b$c";

    Regards,
    Gubendran.L
      The OP wanted to declare the three variables my.
Re: Assign multiple scalars the same value
by g0n (Priest) on Jan 25, 2005 at 09:37 UTC
    I've noticed that

    my ($a,$b,$c) = "TRUE";

    doesn't work

    The reason it doesn't work is that my ($a,$b,$c) is, because of the (), effectively an array. Since "true" is a single element, its equivalent to doing

    my @badger="true"; foreach (@badger){print "$_\n"}

    The following

    my ($a,$b,$c) = ("TRUE", "TRUE", "TRUE");

    works OK, because the variables on both side of the '=' are arrays - note the ()

    Note that I'm not advocating using this last syntax. I agree with diotalevi that it potentially introduces a bug if you don't use enough "TRUE" values. Personally, I favour jdporters syntax, I'm just pointing out why your original failed.

      They're lists, not arrays. It's an important distinction; try a list of different elements versus an array of different elements assigned to lists and scalars on the left hand side with and without parenthesis.

Re: Assign multiple scalars the same value
by DrHyde (Prior) on Jan 25, 2005 at 10:00 UTC
    I find that my ($a,$b,$c) = "TRUE"; works perfectly. You should have said that it doesn't do what you expect.

    In general saying "it doesn't work" is not helpful. It's better to say "it doesn't do what I expected, and I expected it to do $foo".

      The OP said, "I want to do X, but when I try Y, it doesn't work." Now you're saying that Y does work. But you've taken it out of the context of the stated need, X.
Re: Assign multiple scalars the same value
by nobull (Friar) on Jan 25, 2005 at 12:21 UTC

    Others have told you how to do what you ask but I'd like to point out that at least 90% of the time when people think they want declare three or more scalars in the same place and assign then all the the same value they are wrong.

    Usually they are making one or more of the following mistakes:

    • Suffering from premature declaration. (i.e. not declaring all variables in the smallest applicable scope).
    • Using multiple scalar variables where they should be using a hash or array.
    • Representing the concept of undefindness using some value other than undef.
    There's just been an almost identical thread over in comp.lang.perl.misc
Re: Assign multiple scalars the same value
by Anonymous Monk on Jan 25, 2005 at 14:54 UTC
    This was discussed on comp.lang.perl.misc only a few days ago. You can read the thread here.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (11)
As of 2014-12-18 10:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (48 votes), past polls