Perl: How to access a variable via a string

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

GTM
Posts: 90
Joined: Tue Nov 10, 2009 4:53 am UTC

Perl: How to access a variable via a string

Postby GTM » Tue May 20, 2014 9:36 pm UTC

Contriving up an example of what I want:

Say I have 2 variables in perl:
$countera = 0;
$counterb = 0;

and then the user types into the terminal "countera", and I want the counter he types to increase by 1.

I dont want to use 2 if statements as that would not be scalable when I increase the number of counters, is there something in the language that (assuming I checked for errors already) lets me say
"$[what the user typed]++;" ?

EDIT: alternatively, it doesn't have to be user inputted, as that wasnt the point looking back.

$text = "countera";
$[$text]++;

I want something like that. Does that exist?

User avatar
hotaru
Posts: 1040
Joined: Fri Apr 13, 2007 6:54 pm UTC

Re: Perl: How to access a variable via a string

Postby hotaru » Tue May 20, 2014 10:21 pm UTC

GTM wrote:EDIT: alternatively, it doesn't have to be user inputted, as that wasnt the point looking back.

$text = "countera";
$[$text]++;

I want something like that. Does that exist?

what you think you want here is probably something like

Code: Select all

$$text++;


but, that's pretty much never what you want to actually do, because then the user can modify any global variables in your program, which is very bad.

Code: Select all

factorial product enumFromTo 1
isPrime n 
factorial (1) `mod== 1

User avatar
ahammel
My Little Cabbage
Posts: 2135
Joined: Mon Jan 30, 2012 12:46 am UTC
Location: Vancouver BC
Contact:

Re: Perl: How to access a variable via a string

Postby ahammel » Tue May 20, 2014 10:30 pm UTC

Letting the user modify the global state like that sounds really gross to me, but anyway:

Use a hash associating the strings you want with closures that increment the variables?

Code: Select all

my %counters = (
  "countera" => sub { $countera++; },
  # etc
)

$counters{$text}();
 
He/Him/His/Alex
God damn these electric sex pants!

GTM
Posts: 90
Joined: Tue Nov 10, 2009 4:53 am UTC

Re: Perl: How to access a variable via a string

Postby GTM » Tue May 20, 2014 10:44 pm UTC

I'll try these, thanks!

While I don't want multiple if statements, I'm actually keeping a list of the modifiable variables in an array and looping through that with a single if statement, so it's not quite giving the user full control, haha.

User avatar
ahammel
My Little Cabbage
Posts: 2135
Joined: Mon Jan 30, 2012 12:46 am UTC
Location: Vancouver BC
Contact:

Re: Perl: How to access a variable via a string

Postby ahammel » Tue May 20, 2014 10:46 pm UTC

hotaru wrote:

Code: Select all

$$text++;

I think this is a (particularly horrible) PHP-ism.
He/Him/His/Alex
God damn these electric sex pants!

GTM
Posts: 90
Joined: Tue Nov 10, 2009 4:53 am UTC

Re: Perl: How to access a variable via a string

Postby GTM » Tue May 20, 2014 11:08 pm UTC

1st method didnt work, 2nd method is perfect. Thanks!


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 6 guests