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

This seems like such a trivial thing(and it probably is) but for some reason I'm having trouble with this. What I want to do is preset several variables with the same numeric value. ie:
<pseudo-code> $len = length(trim($fld)); $min[$i], $max[$i], $emax[$i] = $len if ! defined $min[$i]; </pseudo-code>

The above does not work of course, but this is the idea. Is there a way to set several variables at once? Thanks!

Replies are listed 'Best First'.
Re: assigning a value to several vars
by broquaint (Abbot) on Jan 16, 2004 at 15:55 UTC
    You could chain the return value of an assignment e.g
    $min[$i] = $max[$i] = $emax[$i] = $len;
    Or you could just repeat the value e.g
    ($min[$i], $max[$i], $emax[$i]) = ($len) x 3;
    Update: or as merlyn might have it
    $_ = $len for $min[$i], $max[$i], $emax[$i];
    See. perlop and perlsyn for more info.
    HTH

    _________
    broquaint

      Or in this particular case,
      $_->[$i] = $len for \@min, \@max, \@emax;

      The PerlMonk tr/// Advocate
Re: assigning a value to several vars
by Abigail-II (Bishop) on Jan 16, 2004 at 15:56 UTC
    $min [$i] = $max [$i] = $emax [$i] = $len; ($min [$i], $max [$i], $emax [$i]) = ($len) x 3; { no strict 'refs'; for my $name (qw /min max emax/) {$$name [$i] = $len} } for my $name (qw /min max emax/) { eval "$name [\$i] = \$len;"; }

    Abigail

Re: assigning a value to several vars
by Zaxo (Archbishop) on Jan 16, 2004 at 20:30 UTC

    Or,

    ($min[$i], $max[$i], $emax[$i]) = ($len) x 3 if ! defined $min[$i];

    After Compline,
    Zaxo