Accessing a hash whose name is constructed

Accessing a hash whose name is constructed

am 15.04.2008 21:46:49 von Gibbering Poster

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?

Re: Accessing a hash whose name is constructed

am 15.04.2008 21:56:53 von xhoster

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

Re: Accessing a hash whose name is constructed

am 15.04.2008 22:08:01 von jurgenex

Gibbering 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

Re: Accessing a hash whose name is constructed

am 15.04.2008 22:09:16 von smallpond

On Apr 15, 3:46 pm, Gibbering 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

Re: Accessing a hash whose name is constructed

am 15.04.2008 23:47:38 von Chris Mattern

On 2008-04-15, Gibbering 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

Re: Accessing a hash whose name is constructed

am 16.04.2008 03:47:21 von Tad J McClellan

Gibbering 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/"