in reply to The "no strict refs" problem revisited.
"Can't use string ("1") as a HASH ref while "strict refs" in use at....." error.
This sounds to me like something's not quite right in the code -- unless you really wanted to assign a value to a key in the %1 hash. If that's not what you intended, the strictures are helping you, by pointing out that your code doesn't really do what you want.
$box{$X}{$Y}{$z} = 1; #dosn't work and causes error below
What do you mean, "doesn't work"? This code works just fine, if by "work" you mean to assign the value 1 to the scalar value $box{$X}{$Y}{$z} (which could otherwise be written as ${${$box{$X}}{$Y}}{$z} or $box{$X}->{$Y}->{$z} and which is the value for key $z within the hash that is the value for key $Y within the hash that is the value for key $X within %box). What did you think this line would do, if not assign a value of the number 1? That's what it does.
# $box{$X}{$Y}{$z}{'rect'} = 1 ; #works
I'm not certain this works the way you think it does. You've just assigned a numeric value to $box{$X}{$Y}{$z}, but now you're trying to treat that value (that *numeric* value, the one that you just assigned, the number 1) as a hash (or a hash reference, technically). 1 is not a hash (nor a hash reference), so this won't work -- straightforwardly. It could work as a symbolic reference, but in that case you'd be assigning to $1{rect} and $1{text}, which I suspect is not what you wanted to do.
If you want $box{$X}{$Y}{$z} to be a hashref, why on earth are you making it a number? A number is not a hash reference. If you want a hashref there, put a hashref there:
$box{$X}{$Y}{$z} = {}; # Assign anonymous hashref. # Or, alternately... %{$box{$X}{$Y}{$z}} = (); # Assign empty list to hash.
The undef trick the other poster mentions will also work, but for slightly less straightforward reasons. It should (hopefully) be obvious why assigning an empty hashref will provide you with a hashref in which you can subsequently create values. Also, assigning a hashref, even if it's empty, will cause the value to be defined (since it will contain the hashref you assigned); assigning undef merely causes it to exist (where "exists" means only that the key is in the hash but doesn't imply that a value is defined for it). If you don't understand this paragraph, don't worry about it for now; if you aren't testing for existence, you probably don't really need to know about it.
I *think* that assigning an empty hashref is what you want to do (though I'm not sure why), but I could be misunderstanding. If so, please explain in more detail what it is you actually want to do, so we can help better.
|
|---|