datannen has asked for the wisdom of the Perl Monks concerning the following question:

Why does...
if (%hash->{$base}->{'Initialized'} != 1) {
give me:
Can't use string ("1") as a HASH ref while "strict refs"

Replies are listed 'Best First'.
Re: simple hash question
by davido (Cardinal) on May 27, 2004 at 04:25 UTC
    I think you've got your sigils wrong. %hash->{$base}->{'Initialized'} should, probably, be something along the lines of: $hash->{$base}->{'Initialized'}. ...depending on what your datastructure looks like.

    Using a %hash as a reference is deprecated.

    Also, it's most likely that you're trying to use the value of 1 as a reference, which isn't legit. Never hurts to have a closer look at what's actually in your datastructure.


    Dave

Re: simple hash question
by antirice (Priest) on May 27, 2004 at 04:30 UTC

    I'm pretty certain if you were to do this:

    use Data::Dumper; print Dumper($hash{$base});

    it would print $VAR1 = 1;. It's exactly what the error message is telling you. You should verify that you're correctly creating your hash. Also, the %hash->{} syntax is deprecated in modern perl.

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

Re: simple hash question
by Enlil (Parson) on May 27, 2004 at 04:36 UTC
    I would bet that for the same reason that:
    perl -Mstrict -we 'my $hash={foo => 1}; print $hash->{"foo"}->{"bar"}'
    prints the same thing. Basically %hash->{$base} does not point to a HASH ref, so when you try to use it as one strict points it out, and as davido points out (as well as -w or warnings): Using a hash as a reference is depreciated.

    -enlil

Re: simple hash question
by wolfi (Scribe) on May 27, 2004 at 04:17 UTC

    you're trying to match a hash again the number 1, yes? (rather than use the 1 as part of the ref).

    try this...

    if (%hash->{$base}->{'Initialized'} !~ /1/) {

    or

    if (%hash->{$base}->{'Initialized'} !~ /"1"/) {

    about strict:
    if i'm not mistaken, most values need to be in quotes, while strict is in place

      use strict; my $hash = { Foo => 1 }; if( $hash->{Foo}->{EEEEEEEK} cmp "abcdefghijklmnopqrstuvwxyz" ){ } __END__ Can't use string ("1") as a HASH ref while "strict refs" in use at ... +.
      about strict: if i'm not mistaken, most values need to be in quotes, while strict is in place
      You are mistaken. If you want to know what strict is for read `perldoc strict'. If you want to know about quoting hash keys, start with `perldoc perldata'.

      MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
      I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
      ** The third rule of perl club is a statement of fact: pod is sexy.

        i meant unquoted strings and not values, while strict was in place.

        As i reread the question, this probably wasn't necessary in this case, as it appears to be more of a mathematical - rather than string - comparison.

        my apologies...

      If his datastructure contains a 1 (a numeric value), there's no reason to resort to using the regexp comparisons. And there's no need to wrap the 1 in quotes.

      You're barking up the wrong tree with those suggestions.

      It is perfectly legit to say:

      if ( $item != 1) {....

      And a properly accessed hash element is not an exception to this rule.


      Dave

Re: simple hash question
by Aragorn (Curate) on May 27, 2004 at 08:23 UTC
    Apparently, your understanding about references to hashes (and arrays, scalars, etc) is lacking. Try reading perlreftut to get up to speed quickly on references.

    Arjen