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!

In reply to Re: multiple variable initialization in one line by Random_Walk
in thread multiple variable initialization in one line by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.