[OT] Refs don't work, like I want
Hello, Sorry about non mod_perl question, but as I'm subscribed to this list and I know I can get help from here, I ask my question here. Can anyone tell me, what's wrong with this piece of code: $vars-{'key2'} = value of second key; $vars = { xxx = AAA, yyy = BBB, zzz = CCC, }; $vars-{'key1'} = value of first key; foreach $a ( keys %{$vars} ) { print $a = $vars-{$a}\n; } Problem is, that value of key2 is lost after I set values to xxx, yyy and zzz, but key1 is ok. I searched through perlref, perldsc manpages, but didn't find anything similar (maybe that's the problem?). Any help is welcome. Rgds, Viljo
Re: [OT] Refs don't work, like I want
On Fri, 17 May 2002 17:10:53 +0300 (EEST) Viljo Marrandi [EMAIL PROTECTED] wrote: : $vars-{'key2'} = value of second key; The hash $vars points to has a key named key2. : $vars = { : xxx = AAA, : yyy = BBB, : zzz = CCC, : }; Now you change the reference stored in $var. It points to an entirely new hash, whose keys are xxx, yyy and zzz. : $vars-{'key1'} = value of first key; Here you add the key key1 to the hash $vars points to. : Problem is, that value of key2 is lost after I set values to xxx, yyy and : zzz, but key1 is ok. $vars contains a reference to a hash that has nothing to do with the first one, you didn't create a key named key2 in that hash. -- fxn
Re: [OT] Refs don't work, like I want
Viljo Marrandi wrote: Sorry about non mod_perl question, but as I'm subscribed to this list and I know I can get help from here, I ask my question here. Please do NOT do that in the future. This is the *modperl* list. There are hundreds of perl lists which will gladly help you out. perlmonks.org is one of these places. Folks, please refrain to reply to this kind of questions, but instead send those who ask to the other resources, *without* answering the question, no matter how simple it is. Don't encourage people to be lazier than they are already. I know I don't sound nice, but we try hard to keep a low Sound to Noise Ratio here. Please help to keep this list useful to those who seek mod_perl help and clean of noise for those who can help. p.s. I usually don't bash this kind of posts, but this one was saying: as I'm subscribed to this list and I know I can get help from here, I ask my question here. it's not very nice of you, Viljo. Please subscribe to other relevant lists and ask the questions there if they don't belong here. __ Stas BekmanJAm_pH -- Just Another mod_perl Hacker http://stason.org/ mod_perl Guide --- http://perl.apache.org mailto:[EMAIL PROTECTED] http://use.perl.org http://apacheweek.com http://modperlbook.org http://apache.org http://ticketmaster.com
Re: [OT] Refs don't work, like I want
Stas Bekman wrote: I know I don't sound nice, but we try hard to keep a low Sound to Noise Ratio here. of course I meant a *high* Sound to Noise Ratio. the heat and humidity shows :) p.s. sorry for adding to the noise. back to work on adding more sound. __ Stas BekmanJAm_pH -- Just Another mod_perl Hacker http://stason.org/ mod_perl Guide --- http://perl.apache.org mailto:[EMAIL PROTECTED] http://use.perl.org http://apacheweek.com http://modperlbook.org http://apache.org http://ticketmaster.com
Re: [OT] Refs don't work, like I want
In support of F. Xavier Noria, and in simpler terms - your $vars = { . } overwrote your previous assignment of $vars-{'key2'}. Perhaps you could have done: my $var = {}; $var-{'key2'} = some value; my @args = qw/ XXX YYY ZZZ /; my @vals = qw/ AAA BBB CCC /; my $i; for ($i =0; $i scalar(@args); $i++) { $vars-{$args[$i]} = $vals[$i]; } $var-{'key1'} = some other value; This would not have overwritten the $var-{'key2'} assignment. --Jon Robison F.Xavier Noria wrote: On Fri, 17 May 2002 17:10:53 +0300 (EEST) Viljo Marrandi [EMAIL PROTECTED] wrote: : $vars-{'key2'} = value of second key; The hash $vars points to has a key named key2. : $vars = { : xxx = AAA, : yyy = BBB, : zzz = CCC, : }; Now you change the reference stored in $var. It points to an entirely new hash, whose keys are xxx, yyy and zzz. : $vars-{'key1'} = value of first key; Here you add the key key1 to the hash $vars points to. : Problem is, that value of key2 is lost after I set values to xxx, yyy and : zzz, but key1 is ok. $vars contains a reference to a hash that has nothing to do with the first one, you didn't create a key named key2 in that hash. -- fxn
Re: [OT] Refs don't work, like I want
At 16:10 17.05.2002, Viljo Marrandi wrote: Hello, Sorry about non mod_perl question, but as I'm subscribed to this list and I know I can get help from here, I ask my question here. Can anyone tell me, what's wrong with this piece of code: $vars-{'key2'} = value of second key; $vars = { xxx = AAA, yyy = BBB, zzz = CCC, }; $vars-{'key1'} = value of first key; foreach $a ( keys %{$vars} ) { print $a = $vars-{$a}\n; } Problem is, that value of key2 is lost after I set values to xxx, yyy and zzz, but key1 is ok. I searched through perlref, perldsc manpages, but didn't find anything similar (maybe that's the problem?). Reea OT, but ok :) You're effectively re-setting your variable here. What you do above is, if we consider simple scalars, the same as: $vars = Foo1; $vars = Foo2; What will be the value of $vars? Foo2 of course. What are your solutions? The easiest would maybe be to do this: @{$vars}{qw/xxx yyy zzz/} = qw/AAA BBB CCC/; Or $vars-{xxx} = 'AAA', etc -- Per Einar Ellefsen [EMAIL PROTECTED]