Accessing a hash whose name is constructed

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?
Gibbering Poster [ Di, 15 April 2008 21:46 ] [ ID #1943172 ]

Re: Accessing a hash whose name is constructed

Gibbering <roblund [at] gmail.com> wrote:
> 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.
xhoster [ Di, 15 April 2008 21:56 ] [ ID #1943175 ]

Re: Accessing a hash whose name is constructed

Gibbering <roblund [at] gmail.com> wrote:
>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
jurgenex [ Di, 15 April 2008 22:08 ] [ ID #1943176 ]

Re: Accessing a hash whose name is constructed

On Apr 15, 3:46 pm, Gibbering <robl... [at] gmail.com> wrote:
> 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
smallpond [ Di, 15 April 2008 22:09 ] [ ID #1943177 ]

Re: Accessing a hash whose name is constructed

On 2008-04-15, Gibbering <roblund [at] gmail.com> wrote:
> I want to access some data from a hash, but want to build that hash's
> name on the fly... here's the code:

For God's sake, *why*?
>
> %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?

By not doing it and using a hash of hashes instead.


--
Christopher Mattern

NOTICE
Thank you for noticing this new notice
Your noticing it has been noted
And will be reported to the authorities
Chris Mattern [ Di, 15 April 2008 23:47 ] [ ID #1943184 ]

Re: Accessing a hash whose name is constructed

Gibbering <roblund [at] gmail.com> wrote:

> 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


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
Tad J McClellan [ Mi, 16 April 2008 03:47 ] [ ID #1944049 ]
Perl » comp.lang.perl.misc » Accessing a hash whose name is constructed

Vorheriges Thema: How to know if data is piped into my script
Nächstes Thema: FAQ 9.8 How do I fetch an HTML file?