in reply to unsure how to use variable
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
|
|---|