Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Stumped on an array ref error...

by Madams (Monk)
on Oct 07, 2001 at 06:39 UTC ( #117259=perlquestion: print w/replies, xml ) Need Help??
Madams has asked for the wisdom of the Perl Monks concerning the following question:

Hey all does any one know why this line

if ((exists $cacheHOA{$sum} ) && ($#{$cacheHOA{$sum}} <= 7)){...}

gives me the following error?

Can't use string ("c:\usr\local\bin\PixNews99\Downl") as an ARRAY ref +while "strict refs" in use at c:\usr\local\bin/ line 308 +, <STDIN> line 4.

I am running under -w and use strict;. %cacheHOA is a hash of arrays $sum is a an md5 hashval being used as a hash key. I don't understand why the string pops up (it is part of a file name for one of the files being checked) as i'm checking here to see if the key exists and that the anon array hanging off the key has seven or less members.

Dudes i'm stumped...

(__) (\/) /-------\/ / | 666 || * ||----||

Replies are listed 'Best First'.
Re: Stumped on an array ref error...
by wog (Curate) on Oct 07, 2001 at 06:49 UTC
    The problem is that for @{$cacheHOA{$sum}} to be an array, then $cacheHOA{$sum} must be an arrayref. You are trying to use $cacheHOA{$sum} as a string and an arrayref, but perl only has it as the last thing it was set has, which appears to be string (the file name) in this case.
      Hey wog

      I don't see where i'm trying to use $cacheHOA{$sum} as a string. Per perlfunc:

      exists EXPR Given an expression that specifies a hash element or array element, re +turns true if the specified element in the hash or array has ever bee +n initialized, even if the corresponding value is undefined. The elem +ent is not autovivified if it doesn't exist.

      That doesn't seem to imply any certain context scalar or other..

      This is just stumping me...
      (__) (\/) /-------\/ / | 666 || * ||----||
        The exsits operation is not the problem. What the error message says is that $cacheHOA{$sum} contains a string, apparently a file name, and when you do $#{$cacheHOA{$sum}} you are trying to use that string as an arrayref. You have not given enough code to show us where you are setting$cacheHOA{$sum} to a string, removing the arrayref that, presumably, was in there.
dereference the array ref
by boo_radley (Parson) on Oct 07, 2001 at 06:51 UTC
    using the last index ($#) requireds an array.
    ($#{$cacheHOA{$sum}} is trying to use it on a scalar, not an array. This is untested, but maybe ($#{@{$cacheHOA{$sum}}} would work.

    Update Well,I'll be damned.

      Hey boo_radley

      Tried your possible fix but that gives me the EXACT same error msg.

      P.S: the last array index operation on that line comes straight from perldsc

      (__) (\/) /-------\/ / | 666 || * ||----||
      For the record, this is not correct. An arrayref can be used where an array name would otherwise appear, and thus Madams syntax is correct.
Re: Stumped on an array ref error...
by Madams (Monk) on Oct 07, 2001 at 08:39 UTC
    It works now! Yeah!!!

    I want to thank wog and boo for their (sp?) help. My fix is here (this link is just so's you don't have to scroll to look for it).

    Thank you for you very helpful and kind assistance.

    P.S: wog that bit about using brackets is kind of a "d'oh..I shoulda knowed that!", i use Tk; alot so that should've reached out and smacked me ;)
    (__) (\/) /-------\/ / | 666 || * ||----||

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://117259]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2018-01-22 13:25 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (233 votes). Check out past polls.