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 ;-) | [reply] [d/l] |
|
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.
| [reply] [d/l] |
|
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.
| [reply] [d/l] |
|
my ($a,$b,$c) = map {'TRUE'}(0 .. 2);
| [reply] [d/l] |
|
$,= " "; $\ = "\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. | [reply] [d/l] |
|
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.
| [reply] [d/l] |
Re: Assign multiple scalars the same value
by jdporter (Paladin) 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. | [reply] [d/l] [select] |
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. | [reply] [d/l] |
|
| [reply] |
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
| [reply] [d/l] |
Re: Assign multiple scalars the same value
by davido (Cardinal) on Jan 25, 2005 at 05:48 UTC
|
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. ;)
| [reply] [d/l] |
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. | [reply] [d/l] [select] |
|
| [reply] |
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. | [reply] |
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". | [reply] [d/l] |
|
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.
| [reply] |
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
| [reply] [d/l] |
|
The OP wanted to declare the three variables my.
| [reply] [d/l] |