Re: creating variable names within a perl script
- From: "Jürgen Exner" <jurgenex@xxxxxxxxxxx>
- Date: Sun, 16 Jul 2006 19:33:20 GMT
bdz wrote:
so i can do something like this:
while (-1){
print "enter a variable: ";
$var =<STDIN>;
chomp($var);
# what i want to do at this point is give this variable a name such
# as $var1, then when the loop comes around again I would like
# to be able to give the next variable a different name but since
# I do not know how many are being entered i cannot create the
# names ahead of time. is there anyway to create variable
# names on the fly in perl ?
Technically this is possible (it is known as symbolic references), but it is
is highly inadvisable. Check the FAQ
perldoc -q "variable name"
or the gazillions of previous postings on this topic for details.
In your particular case why don't you simply use an array? You don't even
have to keep track of the current index, because in
$arr[@arr] = <STDIN>;
@arr will be evaluated to the number of elements in the array which happens
to be one larger than the index of the currently last element and thus
exactly the index you need to place the next element.
jue
.
- Follow-Ups:
- Re: creating variable names within a perl script
- From: John W. Krahn
- Re: creating variable names within a perl script
- References:
- Prev by Date: Re: creating variable names within a perl script
- Next by Date: simulate user activity
- Previous by thread: Re: creating variable names within a perl script
- Next by thread: Re: creating variable names within a perl script
- Index(es):
Relevant Pages
|
|