Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re^2: Assign multiple scalars the same value

by dimar (Curate)
on Jan 24, 2005 at 22:30 UTC ( #424720=note: print w/replies, xml ) Need Help??

in reply to Re: Assign multiple scalars the same value
in thread Assign multiple scalars the same value

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

Looks like time to refactor out all ...

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

Replies are listed 'Best First'.
Re^3: Assign multiple scalars the same value
by Aristotle (Chancellor) on Jan 24, 2005 at 22:52 UTC

    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.

Re^3: Assign multiple scalars the same value
by Limbic~Region (Chancellor) on Jan 25, 2005 at 14:31 UTC
    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://424720]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2016-10-24 20:20 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (309 votes). Check out past polls.