|
Prev: How to know if data is piped into my script
Next: Matching URLs with REs (was "Some questions about q{} and qr{}").
From: Gibbering on 15 Apr 2008 15:46 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?
From: xhoster on 15 Apr 2008 15:56 Gibbering <roblund(a)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.
From: J�rgen Exner on 15 Apr 2008 16:08 Gibbering <roblund(a)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
From: smallpond on 15 Apr 2008 16:09 On Apr 15, 3:46 pm, Gibbering <robl...(a)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
From: Chris Mattern on 15 Apr 2008 17:47
On 2008-04-15, Gibbering <roblund(a)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 |