f



Getting value

Hi,
I am getting a string value from a function .A variable is exist by that 
string value. Need to print the value using the second variable!

Example:

my $log_dir="hello";

my $name=function1();     // $name has value "log_dir"

I want the output as "hello" using the "name" variable.  Is that possible?

TIA,

Svel


0
ksakthiv (1)
7/24/2009 8:39:24 AM
comp.lang.perl.modules 4194 articles. 0 followers. jerrykrinock (6) is leader. Post Follow

3 Replies
462 Views

Similar Articles

[PageSpeed] 19

"Sakthi" <ksakthiv@gmail.com> writes:

> Hi,
> I am getting a string value from a function .A variable is exist by that 
> string value. Need to print the value using the second variable!
>
> Example:
>
> my $log_dir="hello";
>
> my $name=function1();     // $name has value "log_dir"
>
> I want the output as "hello" using the "name" variable.  Is that possible?

No, because $log_dir is a lexical variable. Even if that were not the case,
it would still be a terribly bad idea. Much better to use a hash:

    my %dirs = (
        'log_dir' => 'hello',
    );
    my $name = function1();

    print $dirs{$name}, "\n";

See 'perldoc -q "variable as a variable name"' for details.

sherm--
0
Sherm
7/24/2009 11:59:52 AM
Sherm Pendley <spamtrap@shermpendley.com> writes:
> "Sakthi" <ksakthiv@gmail.com> writes:
>> I am getting a string value from a function .A variable is exist by that 
>> string value. Need to print the value using the second variable!
>>
>> Example:
>>
>> my $log_dir="hello";
>>
>> my $name=function1();     // $name has value "log_dir"
>>
>> I want the output as "hello" using the "name" variable.  Is that possible?
>
> No, because $log_dir is a lexical variable.

Yes, because eval works fine with lexical variables:

#!/usr/bin/perl

use strict;
use warnings;

my $log_dir="hello";
my $name = 'log_dir';

print eval "\$$name", "\n";
# prints "hello"

It works, but it's not a good idea.

>                                             Even if that were not the case,
> it would still be a terribly bad idea. Much better to use a hash:
>
>     my %dirs = (
>         'log_dir' => 'hello',
>     );
>     my $name = function1();
>
>     print $dirs{$name}, "\n";
>
> See 'perldoc -q "variable as a variable name"' for details.

Agreed.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
0
Keith
7/27/2009 9:01:20 PM
Thanks Keith and Sherm.

"Keith Thompson" <kst-u@mib.org> wrote in message 
news:lneis1j0wv.fsf@nuthaus.mib.org...
> Sherm Pendley <spamtrap@shermpendley.com> writes:
>> "Sakthi" <ksakthiv@gmail.com> writes:
>>> I am getting a string value from a function .A variable is exist by that
>>> string value. Need to print the value using the second variable!
>>>
>>> Example:
>>>
>>> my $log_dir="hello";
>>>
>>> my $name=function1();     // $name has value "log_dir"
>>>
>>> I want the output as "hello" using the "name" variable.  Is that 
>>> possible?
>>
>> No, because $log_dir is a lexical variable.
>
> Yes, because eval works fine with lexical variables:
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my $log_dir="hello";
> my $name = 'log_dir';
>
> print eval "\$$name", "\n";
> # prints "hello"
>
> It works, but it's not a good idea.
>
>>                                             Even if that were not the 
>> case,
>> it would still be a terribly bad idea. Much better to use a hash:
>>
>>     my %dirs = (
>>         'log_dir' => 'hello',
>>     );
>>     my $name = function1();
>>
>>     print $dirs{$name}, "\n";
>>
>> See 'perldoc -q "variable as a variable name"' for details.
>
> Agreed.
>
> -- 
> Keith Thompson (The_Other_Keith) kst-u@mib.org 
> <http://www.ghoti.net/~kst>
> Nokia
> "We must do something.  This is something.  Therefore, we must do this."
>    -- Antony Jay and Jonathan Lynn, "Yes Minister" 


0
Sakthi
7/28/2009 9:04:57 AM
Reply: