${$key} or similar

M

Marten Lehmann

Hello,

I'm using "use strict" in the beginning of every script, but for one
certain case, I want to access variables by their names like this:

foreach $type ("billing", "admin", "tech") {
print ${$type};
}

The value shall be the same is if I would call $billing, $admin or
$tech. I can assure that these three variables exist in my script and
although I could maybe rewrite parts if the script to put the values in
a hash, I really would like to know how can I access them without a
hash. Any ideas?

Regards
Marten
 
X

xhoster

Marten Lehmann said:
Hello,

I'm using "use strict" in the beginning of every script, but for one
certain case, I want to access variables by their names like this:

foreach $type ("billing", "admin", "tech") {
print ${$type};
}

foreach $type (\$billing, \$admin, \$tech) {
print ${$type};
}

The value shall be the same is if I would call $billing, $admin or
$tech. I can assure that these three variables exist in my script and
although I could maybe rewrite parts if the script to put the values in
a hash, I really would like to know how can I access them without a
hash. Any ideas?

You could temporarily turn off strictures, or use fully qualified variable
names. But you would have to use them everywhere for those variables, not
just in the foreach loop.

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
M

Marten Lehmann

Hello,
foreach $type (\$billing, \$admin, \$tech) {
print ${$type};
}

I cannot change it, I need to work with strings because I need to print
them also, e.g.

print "$type: ". ${$type}. "\n";
You could temporarily turn off strictures, or use fully qualified variable
names. But you would have to use them everywhere for those variables, not
just in the foreach loop.

Turning strict temporarily off omits the errors, but neither ${$type}
nor $$type returns the expected value. How do I have to write it correctly?

Regards
Marten
 
J

John W. Krahn

Marten said:
I'm using "use strict" in the beginning of every script, but for one
certain case, I want to access variables by their names like this:

foreach $type ("billing", "admin", "tech") {
print ${$type};
}

The value shall be the same is if I would call $billing, $admin or
$tech. I can assure that these three variables exist in my script and
although I could maybe rewrite parts if the script to put the values in
a hash, I really would like to know how can I access them without a
hash. Any ideas?

What you are attempting to do *is* access them through a hash, it's just
not a user defined hash. See the "Symbol Tables" section of perlmod.pod.



John
 
J

Jim Gibson

Marten Lehmann said:
Hello,


I cannot change it, I need to work with strings because I need to print
them also, e.g.

print "$type: ". ${$type}. "\n";


Turning strict temporarily off omits the errors, but neither ${$type}
nor $$type returns the expected value. How do I have to write it correctly?

It does on my system:

% perl -e '$type=q(tech);$tech=q(tech value);print "$type: ${$type}\n";'
tech: tech value

Please post a short program that shows what you are getting on your
system.

To write it correctly, you need to use a hash. Why do you not wish to
use a hash?
 
C

C.DeRykus

Hello,



I cannot change it, I need to work with strings because I need to print
them also, e.g.

print "$type: ". ${$type}. "\n";


Turning strict temporarily off omits the errors, but neither ${$type}
nor $$type returns the expected value. How do I have to write it correctly?

Not recommended for queasy stomachs but you could use string eval:

my $code = 'print ';
for ( qw/billing admin tech/) {
$code .= ('$' . __PACKAGE__
. '::' . "$_,"
);
eval $code; die $? if $?;
 
S

sln

Hello,


I cannot change it, I need to work with strings because I need to print
them also, e.g.

print "$type: ". ${$type}. "\n";


Turning strict temporarily off omits the errors, but neither ${$type}
nor $$type returns the expected value. How do I have to write it correctly?

Regards
Marten

I don't know why you need to do this, or why you don't prefer a hash key
and value approach, but this does it what you want.

sln

---------------
use strict;
use warnings;

my ($billing, $admin, $tech) = ('b','a','t');

foreach my $type ("billing", "admin", "tech") {
eval "print \"$type = \$$type\n\"";
}

__END__

output:

billing = b
admin = a
tech = t
 
M

Michael Carman

Marten said:
Turning strict temporarily off omits the errors, but neither ${$type}
nor $$type returns the expected value. How do I have to write it
correctly?

Symbolic references only work with global variables. You can't use them
with lexical (my) variables.

-mjc
 
M

Michael Carman

Marten said:
I'm using "use strict" in the beginning of every script, but for one
certain case, I want to access variables by their names like this:

foreach $type ("billing", "admin", "tech") {
print ${$type};
}

The value shall be the same is if I would call $billing, $admin or
$tech. I can assure that these three variables exist in my script and
although I could maybe rewrite parts if the script to put the values in
a hash, I really would like to know how can I access them without a
hash. Any ideas?

I'm guessing that you want to avoid a hash because you want to minimize
changes to the rest of your program. How about a hash of references instead?

my %types = (
billing => \$billing,
admin => \$admin,
tech => \$tech,
);

while (my ($type, $ref) = each %types) {
print "$type: $$ref\n";
}

If the order is important, you could use a LoL (list of lists):

my @types = (
['billing' => \$billing],
['admin' => \$admin],
['tech' => \$tech],
);

foreach my $t (@types) {
print "$t->[0]: ${$t->[1]}\n";
}

-mjc
 
S

sln

I don't know why you need to do this, or why you don't prefer a hash key
and value approach, but this does it what you want.

sln

---------------
use strict;
use warnings;

my ($billing, $admin, $tech) = ('b','a','t');

foreach my $type ("billing", "admin", "tech") {
eval "print \"$type = \$$type\n\"";

^^^^^^^^^^^^^^^^^^^^^^^
or
eval 'print ' . '"' . $type . ' = ' . '$' . $type . '\n' . '"';

Might be an easier way of looking at it. Look up eval and see what it does (dynamic code?).

--------------

(more of the same)

my @names = qw(billing admin tech);

for ( @names ) {
eval 'print ' . '"' . $_ . ' = ' . '$' . $_ . '\n"';
}


Good luck

sln
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top