Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Little tricks

How do I use the contents of a variable as the name for another variable? by MikeLacey
Posted: 21 Oct 04 (Edited 16 Mar 09)

This very easy to do in Perl but that doesn't mean you should do it; you probably (almost certainly) shouldn't.

You can do it like this.

$v = 'myscalarvar';
$$v = 1; # same as saying $myscalarvar = 1

But don't do it.

What if you have code like this:

$x = 'hello';
$z = 'x';
$$z = 'goodbye';

What's the value of $x at the end of that?

Use a hash instead - like this:

$mylist{arrive} ='hello';
$mylist{leave} ='goodbye';
$mylist{wait} ='slouch';

That will do exactly as you want and won't collide with other variables.

Back to Perl FAQ Index
Back to Perl Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close