Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

www.webdp.net, Event 9 IIS log failed to write entry, wwwxxx jeffs, Catastrophic failure Unexpected method call sequence. 0x8000ffff (-2147418113)., ksh lock a file, [unixODBC][Driver Manager]Driver's SQLAllocHandle on SQL_HANDLE_DBC failed, sed: -e expression #1, char 1: unterminated address regex, procmail + change subject, w2ksp4.exe download, /proc/kallsyms format

Links

XODOX
Impressum

#1: Accessing a hash whose name is constructed

Posted on 2008-04-15 21:46:49 by 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?

Report this message

#2: Re: Accessing a hash whose name is constructed

Posted on 2008-04-15 21:56:53 by xhoster

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

Report this message

#3: Re: Accessing a hash whose name is constructed

Posted on 2008-04-15 22:08:01 by jurgenex

Gibbering <roblund@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

Report this message

#4: Re: Accessing a hash whose name is constructed

Posted on 2008-04-15 22:09:16 by smallpond

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

Report this message

#5: Re: Accessing a hash whose name is constructed

Posted on 2008-04-15 23:47:38 by Chris Mattern

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

Report this message

#6: Re: Accessing a hash whose name is constructed

Posted on 2008-04-16 03:47:21 by Tad J McClellan

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

Report this message