Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: Assign multiple scalars the same value

by Tanktalus (Canon)
on Jan 24, 2005 at 21:58 UTC ( #424711=note: print w/replies, xml ) Need Help??

in reply to Assign multiple scalars the same value

my ($a, $b, $c) = ('TRUE') x 3;

Always many ways of doing it ;-)

Replies are listed 'Best First'.
Re^2: Assign multiple scalars the same value
by diotalevi (Canon) on Jan 24, 2005 at 22:55 UTC
    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.


      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^2: Assign multiple scalars the same value
by dimar (Curate) on Jan 24, 2005 at 22:30 UTC

    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.

      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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://424711]
MidLifeXis waves.
[stevieb]: man, I absolutely LOVE driving in the worst winter conditions. In the mountainous roads, there are extremely few (if any) other drivers. I love the challenge. I've been doing extreme off-road my whole life, and as I said, I'm always well...
[stevieb]: ...prepared for even the very worst
Discipulus seems a part of the globe very unfrindly with Kawasaki riders..
[thezip]: My doggies love it when it snows
[perldigious]: As long as you are a good "boy scout" stevieb, more power to you... but I like my Jeep, and don't like the idea of rolling it or crashing it in to anything. :-)
[thezip]: Me, not as much.
[perldigious]: Mine too thezip, especially my German Shepherd. He looks like he is about to die of heat exhaustion all summer, so he goes nuts when winter comes and he can play in the snow.
[stevieb]: my main off-road vehicle nowadays is a strengthened mercedes ML320. I'm very hard on it, but I've never had any real issues at all (I love how small and light it is, and it has a very, very good centre of gravity for how I roll)

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (11)
As of 2016-12-06 16:48 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (112 votes). Check out past polls.