Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: Re: Hash of hashes syntax

by bwana147 (Pilgrim)
on Mar 19, 2003 at 08:41 UTC ( #244284=note: print w/replies, xml ) Need Help??

in reply to Re: Hash of hashes syntax
in thread Hash of hashes syntax

I'm afraid your last solution, as concise as it might be, simply does not work. \%h always refers to the same hash. You may well change its content at each loop, you're always modifying the same hash, and all members of %hash_of_hashes refer to it.

You really must create a new hash at each loop. This will do it:

$hash_of_hashes{$rn} = { %h };
(the hash keys and values are flattened into a list, which in turn is used to create an anonymous hash).


Replies are listed 'Best First'.
Re: Re: Re: Hash of hashes syntax
by Enlil (Parson) on Mar 19, 2003 at 09:19 UTC
    I'm afraid your last solution, as concise as it might be, simply does not work. \%h always refers to the same hash

    Not true.

    In this case, it would refer to a different hash. This is because the variable my %h is lexically scoped, and thus it is recreated each time through the loop, and so \%h will point to a different hash each time through the loop. To illustrate what I mean here is some code:

    The differences are subtle, but jdporter's code works.


Re: Re: Re: Hash of hashes syntax
by Chmrr (Vicar) on Mar 19, 2003 at 09:24 UTC

    Good eye, but this actually isn't a problem in this case. Because of the my %h inside of the loop, %h is a different (and new) lexical through each iteration -- thus there is no problem just sticking a reference to it into the hash or array. The problem comes from code like this (nearly directly copied from perldsc):

    my %hash; for $i (1..10) { %hash = somefunc($i); $hash_of_hashes{$rn} = \%hash; # WRONG! }

    Note where the "my" is. See perldsc or perlreftut or perllol for more.

    perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://244284]
[Discipulus]: Tanktalus my comment was about how to fork & join in tk?
[marioroy]: Oh, I've might of missed Discipulus's earlier response. I had gone to bed.
[Tanktalus]: ah, I don't even have the perlmonks site open in my browser :)
Tanktalus is chatting through his cbstats application :)
[marioroy]: Perl is so much fun.
[Discipulus]: Tanktalus i recently started a Meditation about marioroy's MCE suit of modules, but is even better to see a practical question answered than responses to my meditation
[Discipulus]: good night mario!
[Tanktalus]: yeah, I saw you post about MCE - the concept looks really really cool. I wonder how well it plays with Coro :)
[Lady_Aleena]: Hi guys. I asked this earlier but got no answer. Why is this dying at -exec: my @music_times = qx(find ~/Music/Albums/ -type f -iname '*.mp3' -exec mp3info -p "%S\n" {} \;);
[Lady_Aleena]: The error find: missing argument to `-exec'

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2017-04-23 19:51 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (432 votes). Check out past polls.