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

Fellow Monks: I wanted to set all the variables, as shown in the code below to zero.
use strict; my ($JAN,$FEB,$MAR,$APR,$MAY,$JUN,$JUL,$AUG,$SEP,$OCT,$NOV,$DEC)=0; print "JAN: $JAN\n"; print "DEC: $DEC\n";
However these are the results & error that I get:
JAN: 0 Use of uninitialized value in concatenation (.) or string at P:\test37 +9.pl line 7. DEC:
However, it appears that only the first in the list, $JAN, get assigned the zero; all the others appear to be uninitialized.
What subtlety am I missing?
Your help in this matter is greatly appreciated, Thanks!

Replies are listed 'Best First'.
Re: multiple variable initialization in one line
by sweetblood (Prior) on Jan 31, 2005 at 14:09 UTC
    A couple of ways:
    my ($JAN,$FEB,$MAR,$APR,$MAY,$JUN,$JUL,$AUG,$SEP,$OCT,$NOV,$DEC)=0 x 12;
    or
    my $JAN=$FEB=$MAR=$APR=$MAY=$JUN=$JUL=$AUG=$SEP=$OCT=$NOV=$DEC=0;
    Cheers

    Update:
    as jmcnamara points out, in the first example the 0 should be in parens as in:

    my ($JAN,$FEB,$MAR,$APR,$MAY,$JUN,$JUL,$AUG,$SEP,$OCT,$NOV,$DEC)= (0) + x 12;

    Sweetblood

      The second example is not equivalent to the OP code. The my only applies to $JAN.

      my $JAN = my $FEB = my $MAR .... my $DEC = 0; would be better.

      However I suspect another data structure would better suit the OP's ultimate purpose, perhaps an array @Month_Whatever where $Month_Whatever[0] is January's figure etc.

      Cheers,
      R.

      Pereant, qui ante nos nostra dixerunt!

      The first example should be:
      my ($JAN,$FEB,$MAR,$APR,$MAY,$JUN,$JUL,$AUG,$SEP,$OCT,$NOV,$DEC) = (0) + x 12;
      See "Multiplicative Operators" in perlop.

      --
      John.

      Perfection! (and not just a help-less redirect)
      Thanks!... that was exactly what I was looking for. :)
Re: multiple variable initialization in one line
by holli (Abbot) on Jan 31, 2005 at 14:06 UTC
Re: multiple variable initialization in one line
by blazar (Canon) on Jan 31, 2005 at 15:07 UTC
    This is coming up quite frequently lately... as a side note, it's not a real answer to your question, but are you sure you're using the correct data structures for your problem?

    What about a hash?

    my @months=qw/JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC/; my %whatever; $whatever{$_}=0 for @months;
    or
    @whatever{@months}=(0) x @months;
    If you really need to create all those vars then I may suggest to use a "special hash", i.e. to mangle the symbol table. But this is seldom the case and I strongly doubt it is for you, so I'm not proceeding in this sense...
Re: multiple variable initialization in one line
by Roy Johnson (Monsignor) on Jan 31, 2005 at 15:24 UTC
    This way requires no counting of the elements.
    $_ = 0 for my ($JAN, $FEB, $MAR); #etc
    You might very well want to use a hash, though, as blazar suggested.

    Caution: Contents may have been coded under pressure.
Re: multiple variable initialization in one line
by Random_Walk (Prior) on Jan 31, 2005 at 15:33 UTC

    Do you realy want tweleve named variables for your per month data ? You will end up typing those same names again and again. You can also test a variable is initialised before you print it. Here are some code frags that may give some ideas of more comfortable ways to do this.

    #!/usr/local/bin/perl use warnings; use strict; my @Month_Data; # $Month_Data[0] is January's data $month_data[11] is December's $Month_Data[0]=0; $Month_Data[1]=5; print "\nFrom an array\n"; print "January : ", defined $Month_Data[0] ? $Month_Data[0] : 0, " wi +dgets\n"; print "February: ", defined $Month_Data[1] ? $Month_Data[1] : 0, " wi +dgets\n"; print "December: ", defined $Month_Data[11] ? $Month_Data[11] : 0, " w +idgets\n"; # with names print "\nFrom synchronised arrays with names\n"; my @Month_Names=qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); for my $i (0..$#Month_Names) { print "$Month_Names[$i]: " , defined $Month_Data[$i] ? $Month_Data +[$i] : 0, " widgets\n"; } # A structure (Array of Arrays) my @Structure; foreach my $Month (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec) +) { push @Structure, [$Month, 0] } print "\nFrom an array of arrays, we zero initialised these so no need + to check defined\n"; foreach my $Month_Data (@Structure) { print "$Month_Data->[0]: $Month_Data->[1] widgets\n"; } # Finaly a hash, order is lost but lookup by month is easy my %Month_Records; foreach my $Month (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec) +) { $Month_Records{$Month}=0 } print "\nFrom a hash, order is lost\n"; foreach my $Month (keys %Month_Records) { print "$Month: $Month_Records{$Month} widgets\n"; } print "\nBut lookup by any random month is easy\n"; print "July: $Month_Records{Jul} widgets\n"; __END__ From an array January : 0 widgets February: 5 widgets December: 0 widgets From synchronised arrays with names Jan: 0 widgets Feb: 5 widgets Mar: 0 widgets Apr: 0 widgets May: 0 widgets Jun: 0 widgets Jul: 0 widgets Aug: 0 widgets Sep: 0 widgets Oct: 0 widgets Nov: 0 widgets Dec: 0 widgets From an array of arrays, we know all these are zero initialised Jan: 0 widgets Feb: 0 widgets Mar: 0 widgets Apr: 0 widgets May: 0 widgets Jun: 0 widgets Jul: 0 widgets Aug: 0 widgets Sep: 0 widgets Oct: 0 widgets Nov: 0 widgets Dec: 0 widgets From a hash, order is lost Mar: 0 widgets Nov: 0 widgets Apr: 0 widgets Oct: 0 widgets May: 0 widgets Sep: 0 widgets Jan: 0 widgets Jul: 0 widgets Dec: 0 widgets Feb: 0 widgets Jun: 0 widgets Aug: 0 widgets But lookup by any random month is easy July: 0 widgets

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!
Re: multiple variable initialization in one line
by sh1tn (Priest) on Jan 31, 2005 at 18:15 UTC
    use strict; use warnings; my $tmp; map { $_ = ++$tmp } my ($JAN,$FEB,$MAR,$APR,$MAY,$JUN,$JUL,$AUG,$SEP,$ +OCT,$NOV,$DEC);