ShermW0829 has asked for the wisdom of the Perl Monks concerning the following question:
Name: Sherman 71 years old retired and creating my own poker visits database
Platform: HP Compaq 6710b
Operating System: Ubuntu 17.10
PERL Version: 5.26
postgresql Version: 9.6
Problem: Need to assign a string to act as a variable
I am building a front end for postgresql entries. I have fourteen entries that I need to query the user and set the user's value into a variable. I am using IO::Prompt.
If I straight-line the code everything works. e.g:
my ($date_in, $date_out); $date_in - prompt("Start Date: "); $date_in =~ s/^\s+|\s+$//g; $date_out = prompt("$End Date: "); $date_out =~ s/^\s+|\s+$//g;
And 12 more user inputs to get along with the no space check equals 24 more lines of code. Lots of code so I tried the following:
my ($k, $date_in, $date_out); my %var_list = ('date_in'=>'Start Date', 'date_out'=>'End Date'); foreach $k (keys(%var_list)) { ## $k prints date_out and then date_in print( "\$k: \"$k\"\n" ); ## $var_list prints End Date and then Start Date print( "\$var_list{k}: \"$var_list{$k}\"\n" ); ## Below is where I want to go: ## $k = prompt($var_list{k}); ## and have $date_out = user's entry ## ## Now how do I assign a beginning $ ## to change date_out to $date_out? ## The below does not work ## ${$k} = prompt(${$var_list{k}}); ## ## I've tried $$k, $($k), and ${$k} ## An example error is the following: ## Can't use string ("date_out") as a SCALAR ## ref while "strict refs" in use at ## ./build_sql_entries line 25 }
Thank you;
Sherman
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Convert string to variable
by choroba (Cardinal) on Feb 16, 2018 at 20:37 UTC | |
by stevieb (Canon) on Feb 16, 2018 at 22:53 UTC | |
|
Re: Convert string to variable
by holli (Abbot) on Feb 16, 2018 at 22:20 UTC | |
by GrandFather (Saint) on Feb 18, 2018 at 20:38 UTC | |
by karlgoethebier (Abbot) on Feb 17, 2018 at 12:41 UTC | |
|
Re: Convert string to variable
by harangzsolt33 (Deacon) on Feb 16, 2018 at 20:35 UTC | |
|
Re: Convert string to variable
by Anonymous Monk on Feb 17, 2018 at 13:30 UTC |