in reply to define variable name on the way
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: define variable name on the way
by amon (Scribe) on Apr 27, 2014 at 18:13 UTC | |
|
Re^2: define variable name on the way
by amon (Scribe) on Apr 27, 2014 at 18:15 UTC | |
by AnomalousMonk (Archbishop) on Apr 27, 2014 at 19:13 UTC |