On Apr 25, 2004, at 1:40 PM, Andrew Gaffney wrote:
James Edward Gray II wrote:
On Apr 25, 2004, at 12:56 PM, Andrew Gaffney wrote:
I'm writing a program that uses a hashref tree to store data. I'm just playing around with this, so it's nothing critical. I remember reading that you could use a string to specify a variable name or something similar. I have the following string value:
my $node = "$data->{computer}->{test}->{item1}->{text}";
This wouldn't be using a string for a variable name, it would be using a string to hold a variable name, some keys and a little syntax or using a sting to hold some Perl code.
You could always eval() it to get the answer, but I seriously doubt that's called for, by what I understood of the problem description.
eval() does work for this. I never thought of that. Is there another way, though?
More simply, you could get the answer by removing the quotes in the line.
That is only an example. In the finished program, that string will be put together dynamically from user input.
I'm with you now. See if this program gives you the needed ideas:
#!/usr/bin/perl
use strict; use warnings;
my $data = {computer => {test => {item1 => {text => "computer.test.item1"}}}};
print fetch(qw(computer test item1 text)), "\n";
sub fetch {
my @keys = @_;
my $node = $data;
while (my $key = shift @keys) {
if (exists $node->{$key}) { $node = $node->{$key}; }
else { return; }
}
return $node;
}
__END__
Hope that helps.
But is that faster than using eval()?
-- Andrew Gaffney Network Administrator Skyline Aeronautics, LLC. 636-357-1548
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>