creating variable names within a perl script

B

bdz

This has been bothering me for years and I think my problem is I do not
know the proper way to ask the question.

Say I want to create an open ended set of variables - I do not know how
many I will have when the script asks me to start entering them.

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 ?

exit if $var eq '-1
}
 
D

Dr.Ruud

bdz schreef:
This has been bothering me for years and I think my problem is I do
not know the proper way to ask the question.

Say I want to create an open ended set of variables - I do not know
how many I will have when the script asks me to start entering them.

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 ?

exit if $var eq '-1
}

Use an array? perldoc -f push
 
J

Jürgen Exner

bdz said:
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
 
J

John W. Krahn

Jürgen Exner said:
bdz said:
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.

Or just use push:

push @arr, scalar <STDIN>;




John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top