Main Page | Report this Page
Computers Forum Index  »  Computer Languages (Perl - Modules)  »  Getting value...
Page 1 of 1    

Getting value...

Author Message
Sakthi...
Posted: Fri Jul 24, 2009 12:51 pm
Guest
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
 
Sherm Pendley...
Posted: Fri Jul 24, 2009 3:59 pm
Guest
"Sakthi" <ksakthiv at (no spam) gmail.com> writes:

Quote:
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--
 
Keith Thompson...
Posted: Tue Jul 28, 2009 1:01 am
Guest
Sherm Pendley <spamtrap at (no spam) shermpendley.com> writes:
Quote:
"Sakthi" <ksakthiv at (no spam) 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.

Quote:
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 at (no spam) 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"
 
Sakthi...
Posted: Tue Jul 28, 2009 1:16 pm
Guest
Thanks Keith and Sherm.

"Keith Thompson" <kst-u at (no spam) mib.org> wrote in message
news:lneis1j0wv.fsf at (no spam) nuthaus.mib.org...
Quote:
Sherm Pendley <spamtrap at (no spam) shermpendley.com> writes:
"Sakthi" <ksakthiv at (no spam) 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 at (no spam) 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"
 
 
Page 1 of 1    
All times are GMT
The time now is Thu Nov 26, 2009 2:00 am