Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

How can I create an array that holds number of variables with values?

( #47523=categorized question: print w/ replies, xml ) Need Help??
Contributed by Anonymous Monk on Dec 20, 2000 at 06:11 UTC
Q&A  > arrays


Description:

Simple question:
I need to initialize 20 variables($var1..$var20) to zero.
$var1=0; $var2=0; $var3=0; ...

How do I do this with arrays in Perl?

Thanks...Mike

Answer: How can I create an array that holds number of variables with values?
contributed by runrig

my @array = map {0} 0..19;

Answer: How can I create an array that holds number of variables with values?
contributed by Russ

Your question almost answers itself

my @var = (43, 60, 1..18); # Later... print $var[1], $var[14];
Use an array instead of multiple, distinct variables.

turnstep is right, you don't have to initialize all the variables to zero, just create them where you need them, giving a value at that time.

runrig's code (my @array = map {0} 0..19) works fine here, as well.

Answer: How can I create an array that holds number of variables with values?
contributed by turnstep

Also keep in mind that you may not need to initialize them at all - perl can usually figure out what you want to do. For example

my $var1; $var1++; print "Var1 is now $var1\n"; my $var2; $var2 = "Foobar"; print "Var2 is now $var2\n";
Both of the above will work fine. The values start out undefined, and in most cases (but not all) they do not need an explicit initialization.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (9)
    As of 2014-12-28 06:02 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (178 votes), past polls