"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.


;$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$;[-1]->();print

In reply to Re: The "no strict refs" problem revisited. by jonadab
in thread The "no strict refs" problem revisited. by zentara

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.