Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > creating variable names within a perl script

Reply
Thread Tools

creating variable names within a perl script

 
 
bdz
Guest
Posts: n/a
 
      07-16-2006
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
}

 
Reply With Quote
 
 
 
 
Dr.Ruud
Guest
Posts: n/a
 
      07-16-2006
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

--
Affijn, Ruud

"Gewoon is een tijger."


 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      07-16-2006
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


 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      07-16-2006
Jürgen Exner wrote:
> 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.


Or just use push:

push @arr, scalar <STDIN>;




John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble running Perl script from within a Perl script laredotornado@zipmail.com Perl Misc 4 07-29-2011 01:44 PM
checking for mis-spelled variable names / function names News123 Python 2 11-26-2008 12:37 AM
shell script with arguments within perl script jay Ruby 7 05-03-2007 08:56 AM
confusion between global names and instantiated object variable names wanwan Python 3 10-14-2005 09:46 PM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments