What you are trying to do is implementing arrays (a very common and basic language feature). You want to execute a string so to speak. I'll tell you how that can be done, but then I tell you how to do it better, easier and much less errorprone:

There is a function called eval() that allows you to execute strings. So you could simply write

$tempvar= "\$holding$count"; eval " print $tempvar ";

Now really, don't do this. eval is an instrument to do things you can't do any other way, sort of like a hominicidal maniac police officer you only give cases where everyone else failed ;-).

Instead use arrays. You could write it this way:

use warnings; $holding[0]="green"; $holding[1]="blue"; $holding[2]="yellow"; # or shorter: @holding= ("green","blue","yellow"); for ($count = 2; $count >= 0; $count--) { print $holding[$count]; #see, no need for a temporary variable }

Also, always put a line "use warnings;" at the start of your script. It will tell you when you are doing something strange and will try to help you pinpoint the trouble. Case in point: It would have warned you that there is a problem with your original script

You might like to read up on arrays in a perl book or on the internet. A feature which might confuse someone new to perl is that you write @xyz if you want to do something with the whole array, but $xyz... if you want to handle just one value from it.

UPDATE: As oko1 said, arrays start at 0, so I had to change the loop a bit


In reply to Re: unsure how to use variable by jethro
in thread unsure how to use variable by dgoroski

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.