Hey all,
perl - how can we name a variable base on the value of another variable?
for example in ksh/bash we do :
export c="100"
export x`echo $c`=2000
echo $x100
x100=2000
is it possible to do something similar for perl?
I already tried many ways but nothing is working.
I am trying to solve a issue in an perl script and the solution that i am trying to do should have different variables.
Can someone please help me on this?
Thanks in advance.
Carlos
you can do it with a hash ref:
my $hashref = {};
$hashref->{var1} = 'val1';
my $var1 = $hashref->{var1};
$hashref->{$var1}= 'val2';
my $var2 = $hashref->{$var1};
print "KEYS:\n";
foreach my $key (keys %$hashref)
{
print "key=".$key."\n";
}
print "\nVALUES:\n";
print "var1=".$var1."\n";
print "var2=".$var2."\n";
this results in
KEYS:
key=var1
key=val1
VALUES:
var1=val1
var2=val2
daptal
3
perl -e 'my $x= 100;
> ${x.$x} = 2000;
> print "$x \n";
> print "$x100 \n";
> '
100
2000
Note: This is not a standard method of doing. When requirement like these arises it is preferable to use hashes
Cheers
Thank you guys!!
It is working