Accessing a hash whose name is constructed

G

Gibbering

I want to access some data from a hash, but want to build that hash's
name on the fly... here's the code:

%hello = (a => 'doggy');
print ${'hell' . lc 'O'}{a};

does the trick as does:
print %{'hell' . lc 'O'}->{a};

however, under "use strict", this fails, since %hello isn't declared
with "my".

If I do put a my in front of the %hello declaration, the print
statement gives me nothing.
I have a sinking suspicion that the above code is wrong, dangerous,
and error prone since I'm not even sure why it works.

What is the proper way to do such a thing?
 
X

xhoster

Gibbering said:
I want to access some data from a hash, but want to build that hash's
name on the fly... here's the code:

Most likely, you want a multi-level hash.
%hello = (a => 'doggy');
print ${'hell' . lc 'O'}{a};

does the trick as does:
print %{'hell' . lc 'O'}->{a};

my %hash;
$hash{hello} = {a => 'doggy'};

print $hash{'hell' . lc 'O'}->{a};

however, under "use strict", this fails, since %hello isn't declared
with "my".

Yes indeed, that is one the of things that use strict is there for. The
main one, even.
If I do put a my in front of the %hello declaration, the print
statement gives me nothing.

You are using two different variables. The lexical %hello, and the global
%main::hello. Since lexicals can't be accessed symbolically, your attempt
at symbolic access (in the absence of use strict) resolved to %main::hello.

Xho

--
-------------------- 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.
 
J

Jürgen Exner

Gibbering said:
I want to access some data from a hash, but want to build that hash's
name on the fly... here's the code:

%hello = (a => 'doggy');
print ${'hell' . lc 'O'}{a};

does the trick as does:
print %{'hell' . lc 'O'}->{a};
I have a sinking suspicion that the above code is wrong, dangerous,
and error prone since I'm not even sure why it works.

See gazillions of previous articles about 'symbolic reference'.
Or just perldoc -q variable ' How can I use a variable as a variable
name?'
What is the proper way to do such a thing?

Use your own hash instead of the system symbol table. In your case
becasue that would become a HoH (hash of [ref to] hash).

jue
 
S

smallpond

I want to access some data from a hash, but want to build that hash's
name on the fly... here's the code:

%hello = (a => 'doggy');
print ${'hell' . lc 'O'}{a};

does the trick as does:
print %{'hell' . lc 'O'}->{a};

however, under "use strict", this fails, since %hello isn't declared
with "my".

If I do put a my in front of the %hello declaration, the print
statement gives me nothing.
I have a sinking suspicion that the above code is wrong, dangerous,
and error prone since I'm not even sure why it works.

What is the proper way to do such a thing?

This should answer some of your questions
perldoc -q 'How can I use a variable as a variable name?'

But why name this hash at all? Why not use an anonymous hash?
To be useful, you need a reference to it anyway.
--S
 
T

Tad J McClellan

Gibbering said:
I want to access some data from a hash, but want to build that hash's
name on the fly...


I suggest that it would be of great benefit to you to
stop wanting that...

here's the code:

%hello = (a => 'doggy');
print ${'hell' . lc 'O'}{a};

does the trick as does:
print %{'hell' . lc 'O'}->{a};

however, under "use strict", this fails, since %hello isn't declared
with "my".

If I do put a my in front of the %hello declaration, the print
statement gives me nothing.
I have a sinking suspicion that the above code is wrong, dangerous,
and error prone since I'm not even sure why it works.


Because Perl has two separate systems of variables.

Your code as shown uses "package variables" while my()
declares "lexical variables" instead.

See:

"Coping with Scoping":

http://perl.plover.com/FAQs/Namespaces.html
 

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,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top