Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get variable value

Status
Not open for further replies.

Jboy123

MIS
Jun 8, 2004
24
0
0
CH
Hi

I'm having a problem where a variable is not being interpreted instead the name of the variable is being displayed.

sub one
{
# Some code here that sets following variable
$wcnode="avalue"
}
sub two
{
$hostname=myhost;

#Read in a config file with key/value pairs separated by '='
foreach $line (@configfile)
{
($key, $value) = split(/\=/, $line);
chomp($key);
chomp($value);
%myhostnameHash->{$key} = $value;
}
# Then set a variable that is equal to a variable defined earlier in script
$installdir = "$myhostnameHash{$hostname}";
print "Value of installdir is $installdir\n";

}

# Main
&sub_one;
&sub_two;

$configfile is a flat text file that contains e.g. the following:

myhost=$wcnode

When I call the print statement in 'sub two' what is returned is:
$wcnode
Instead of the value of $wcnode - 'avalue'

I've set a variable to another variable many times before without any problems. E.g $x = $y

Is there something I need to do to to evaluate the variable??

Any help appreciated
Thanks!
 
try "use strict" to double-check code
what is "myhost" ?
 
Hi

Tried use strict but just got back a page full of messages along lines of:

Global symbol "$variable" requires explicit package name at ./myscript.pl line 46.

Is ther something I should be looking for?

$hostname = "myhost";
myhost is just a value.

Thanks
 
I noticed in your script you have this:

Code:
%myhostnameHash->{$key} = $value;

Shouldn't that be this?

Code:
$myhostnameHash{$key} = $value;
 
Thanks Teriviret

Both seem to work.

$myhostnameHash{$key} = $value;
%myhostnameHash->{$key} = $value;

This is the first time I've been playing around with hashes so I'm not really sure what the difference in the two lines is.

When I retrieve the key from the has the correct value is returned - the problem remains that I want that value to be a variable name which when I use it later in the script the variable is not being interpreted - just the name is returned (in the print statement).
 
It sounds like you want to de-reference a symbolic reference. Something like this?

Code:
$abc = '123';
$def = 'abc';
print "$abc\n";  # prints:  123
print "$def\n";  # prints:  abc
print "$$def\n"; # prints:  123
 
Hi

I'm still getting the same issue:

I think the problem is with :

# Then set a variable that is equal to a variable defined earlier in script
$installdir = "$myhostnameHash{$hostname}";
print "Value of installdir is $installdir\n";

What I want to achieve is:

$installdir = $wcnode; # Where $wcnode is a variable previously defined in the script with a value of, for example, 'avalue'.

The value for the key $myhostnameHash{$hostname} is actually a string value of $wcnode. I sucessfully reteive this from the hash. But my problem is that $installdir gets a value of the string $wcnode not the actual value of the variable $wcnode i.e. 'avalue'

Does this make any more sense?

Thanks
 
Okay

Got it to work by using eval

$installdir = eval "$myhostnameHash{$hostname}";

Thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top