Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

define variable name on the way

by madM (Beadle)
on Apr 27, 2014 at 17:50 UTC ( #1084010=perlquestion: print w/replies, xml ) Need Help??

madM has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I was wondering if there is any options to define the variable name of for example a string when the script runs. For example something like this

$number =1;
$name.$number= 'hey';

so the variable with the string 'hey' would be $name1

is this or something similar possible
i appreaciate your answers

Replies are listed 'Best First'.
Re: define variable name on the way
by amon (Scribe) on Apr 27, 2014 at 18:10 UTC

    There is a feature similar to this in Perl, but this is considered to be a very bad idea, because it makes code utterly unmaintainable. Use arrays instead.

    my $index = 0; my @array; $array[0] = "hello"; $array[1] = "world"; print "$names[0] $names[1]"; #=> "hello world"

    You can also use variables for the 0 or 1: my $index = 0; $array[$index] = "hello"

    Or you could use hashes

    my $index = 0; my %hash; $hash{greeting} = "hello"; $hash{name} = "world"; print "$hash{greeting} $hash{name}"; #=> "hello world"

    Again, you could also use variables:

    my $key = "greeting"; $hash{$key} = "hello";

    Edit: properly escaped square brackets

      The canonical explanation why you shouldn't use variables as variable names was given by MJD in the varvarname emails.

      Also, the above formatting got b0rked. The second paragraph should be:

      You can also use variables for the 0 or 1: my $index = 0; $array[$index] = "hello"

        Because you posted as a registered user, you can un-b0rk your own original post by editing it, in this case by adding  <code> ... </code> tags. Please see Writeup Formatting Tips and Markup in the Monastery.

        Update: If you edit or update one of your posts, please add a brief notation of the edit and its nature.

Re: define variable name on the way
by NetWallah (Canon) on Apr 27, 2014 at 18:12 UTC
Re: define variable name on the way
by Laurent_R (Canon) on Apr 27, 2014 at 21:45 UTC
    The bottom line is that symbolic references can almost always be replaced with an hash. I once tried symbolic references because I had the feeling that it was difficult to do otherwise, only to find out finally that I only needed a hash of hashes to solve my problem. Having gone through that dilemma, I think I can find a better solution for just about any problem, I can't think of a real reason where symbolic references would really make sense.
Re: define variable name on the way
by bart (Canon) on Apr 28, 2014 at 20:28 UTC

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1084010]
Approved by marto
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2023-06-09 13:53 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (36 votes). Check out past polls.