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

Re: Re: Stumped on an array ref error...

by Madams (Monk)
on Oct 07, 2001 at 07:17 UTC ( #117265=note: print w/replies, xml ) Need Help??

in reply to Re: Stumped on an array ref error...
in thread Stumped on an array ref error...

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 || * ||----||

Replies are listed 'Best First'.
Re: Re: Re: Stumped on an array ref error...
by wog (Curate) on Oct 07, 2001 at 07:21 UTC
    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.
      Thanks wog,

      Your response about the exists operation lead me to really look at where the array ref is installed. I changed the following line (where ever it occured):


      to 2 separate lines:

      $cacheHOA{$sum}=(); push @{$cacheHOA{$sum}}, $name;

      and now it works, obviously i thought i was (in the one line version) assigning an annon array with one member, which also obviously did not work (why not?).

      In the two line version i explicitly assigned an empty list and then pushed the member in.

      Thanks for letting me bounce this off of y'all! Sometimes all you need is to have someone look at what your doing and say "Dude, you fscked that right here!"(points and smacks your head...)
      (__) (\/) /-------\/ / | 666 || * ||----||
        $cacheHOA{$sum}=($name) assigns $name to $cacheHOA{$sum}. To actually create an arrayref you need to use []s instead, like $cacheHOA{$sum}=[$name].
        Scalar -> one, Array/List -> many.

        Take this line of code:

        There's a scalar on the left side and a one-element list on the right side. The left side determines the context. A hash value can only hold one thing, so it's scalar context. Perl evaluates the right side in scalar context.

        The Camel makes it abundantly clear that there is no general rule for this type of evaluation. Contrast these two snippets:

        $cacheHOA{$sum} = (1, 2, 3); $cacheHOA{$sum} = @values;
        The same goes for this line:
        Contextually, it's the same thing. The list is just empty this time. However, hash autovivification does the right thing afterwards because of the current value of $cacheHOA{$sum} at that point. (Assign '1' to it instead of an empty list and see the warning this next line throws.)
        push @{$cacheHOA{$sum}}, $name;
        Perl reads that as 'treat whatever is in $cacheHOA{$sum} as an array reference, and stick $name on the end'. Different context, since there's no scalar assignment on the left side.

        The trick is to make the right side of the assignment something that makes sense in scalar context. Since references are scalars, that's what you need. Either assign an array reference or an reference to an existing array.

        $cacheHOA{$sum} = []; $cacheHOA{$sum} = [$name]; $cacheHOA{$sum} = \@names;
        When you don't get what you expect, check the context.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://117265]
[harishnv]: hey

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2018-02-18 10:58 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (253 votes). Check out past polls.