From: "Mr. Shawn H. Corey" <[EMAIL PROTECTED]> > On Wed, 2008-04-09 at 00:21 +0200, Jenda Krynicky wrote: > > From: [EMAIL PROTECTED] > > > I want to store a hash into an array: e.g.: like this > > > > > > #!/usr/bin/perl -v > > > > > > print "Content-type: text/html\n\n"; > > > > > > $h{'hello'}="hello"; > > > > This accesses the hash named %h. > > I'm confused by this. "Access" normally means read, not assign. A more > accurate statement is, "This modifies the hash '%h'."
Access means either read, assign or delete. > > > $h{'by'}="by"; > > > $a[0]=$h; > > > > This accesses the scalar named $h. > > > > Those two are NOT related. > > > > You should start your scripts with > > > > use strict; > > use warnings; > > > > to be shielded from this kind of errors. > > Ah, no. This shields you from errors like typos. It does not protect > you from lack of experience. To gain experience, you have to > experience. In this case the reason for unexpected access to scalar $h was not caused by a typo, but rather by a misunderstanding of the sigils in Perl. In either case the "use strict" would notify the developer that they are attempting to access a different variable than they intended to. Whether it would be the %h accessed by $h{'hello'} or the $h accessed by the $a[0]=$h depends on the way he would declare the variable, but the main point is that he would be warned that each of those lines accesses a different variable. Of course "use strict" can't protect you from all mistakes commited due to the lack of experience, but it can prevent many. If only by preventing you from running the script until you read some more docs and clear some confusion. > > In this case if you want the $a[0] to contain a reference to the hash > > %h, you have to do it like this: > > > > $a[0] = \%h; > > If you look at my previous post, you will see that this is not the only > way to interpret what the OP wants. He wants to store a hash in an array. Maybe one of us should have reminded him that he can only store a hash reference in an array, but I do not see another way to interpret the OP's wants. > As always, when somebody post a message requesting help, they post just > a tiny bit of what is causing them problems. And since we, those who > can answer they problems, only have a tiny glimpse of what they're > really talking about, it would be presumptuous of us to assume that they > could mean only one thing (or one methodology). > > And no, I'm not picking on you specifically; they are many here who > answer questions and think they are right and "My way is THE ONLY WAY." > > Take this problem for example. The OP has assigned a reference to a > hash to the first element of an array. So what does the rest of the > array look like? Is it an array of hash references? Or is it an array > of mixed types? If there is only one of them, then why did he choose an > array rather than a scalar? And this is relevant how? > These questions cannot be answer without seeing more of the code. When > one answers a question, one should not assume there is only one answer > to them or that one's answer is THE ONLY WAY. I don't see why should we answer them. You can only answer the questions asked plus sometimes a little more. You can't answer all question that the OP might have needed to ask. Jenda ===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz ===== When it comes to wine, women and song, wizards are allowed to get drunk and croon as much as they like. -- Terry Pratchett in Sourcery -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/