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

I get this error message when I try to run my script, but not when I use perl -c...

Can't use string ("1") as a HASH ref while "strict refs" in use at ./UsageCount.pl line 230, <USAGE_FILE> chunk 3.


Here's line 230...

$sw_ctype_brkdown{$switchnum}{$calltype}{seconds} += $seconds;

$switchnum and $calltype are both valid scalar values and I use them as indexes to the same hash at other points in the script.
seconds is just what I want the index for that dimension of the hash to be.
$seconds is a valid scalar with a number in it.
  • Comment on Can't use string "1" as a HASH ref while "strict refs" in use

Replies are listed 'Best First'.
Re: Can't use string "1" as a HASH ref while "strict refs" in use
by Chmrr (Vicar) on Mar 04, 2002 at 18:24 UTC

    It's trying to tell you one of the following; either $sw_ctype_brkdown{$switchnum}{$calltype} = 1; or $sw_ctype_brkdown{$switchnum} = 1; It's impossible to tell which at this point. In any case, you're treating something as a hash reference which is really the number 1. Data::Dumper might be your friend in this case, to help track down what your data structure actually looks like.

    perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

      $sw_ctype_brkdown{$switchnum}{$calltype} does equal 1, it's an increme +nted count of how many lines in a file I'm parsing have that $switchn +um and that $calltype. The $switchnum and $calltype are defined right before the problem line + with a substr of a line in the file. I want to be able to get a count of how many lines there are with that + $switchnum $calltype using print "$sw_ctype_brkdown{$switchnum}{$calltype}\n"; and the number of seconds for that $switchnum and $calltype using print "$sw_ctype_brkdown{$switchnum}{$calltype}{seconds}\n" Should I give the value of the seconds to a completely separate hash w +hich will use the same first two indexes? Thanks, Vince

        You need to choose one or the other -- either it's a scalar, or it's a hash reference. It can't be both.

        That said, you're probalby better off with it as a hash reference. That is, make your c"ount of how many lines" be $sw_ctype_brkdown{$switchnum}{$calltype}{lines} and your count of the "nuber of seconds" be $sw_ctype_brkdown{$switchnum}{$calltype}{seconds} -- this way, you keep to one data structure, and it's a structure which explains itself. This is always a plus.

        perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

Re: Can't use string "1" as a HASH ref while "strict refs" in use
by tradez (Pilgrim) on Mar 04, 2002 at 19:22 UTC
    one of those is simply a scalar. to find out which add this code precursing that line
    print "ref of switchnum = ref($switchnum) \n ref of calltype = ref($calltype) \n";
    The one that is ne "HASH" is your culprate

    Tradez
    "Never underestimate the predicability of stupidity"
    - Bullet Tooth Tony, Snatch (2001)
      Let's try:
      my $switchnum = 15; my $calltype = []; # Empty array ref print "ref of switchnum = ref($switchnum) \n ref of calltype = ref($calltype) \n";
      It prints:
      ref of switchnum = ref(15) ref of calltype = ref(ARRAY(0x1234567))


      Guess what: you can't interpolate functions that way :)

      Try this one:
      ref(eval "\$$_") or warn "\$$_ is not a reference" for qw(switchnum ca +lltype);

      ++ vs lbh qrpbqrq guvf hfvat n ge va Crey :)
      Nabgure bar vs lbh qvq fb jvgubhg ernqvat n znahny svefg.
      -- vs lbh hfrq OFQ pnrfne ;)
          - Whreq