in reply to Re: Re^4: Counting keys with defined or undefined elements in a hash (behaviour of values())
in thread Counting keys with defined or undefined elements in a hash

I think that just throws the created scalar away, but doesn't avoid its creation. The function is not only copying the values, it also has to keep track of its iterator, not to mention and requires a function call for every pair in the hash, rather than a single one for all of them. It does use less memory though, yes, that's what I meant when I said it's desirable when dealing with large hashes.

Makeshifts last the longest.

  • Comment on Re^6: Counting keys with defined or undefined elements in a hash (behaviour of values())

Replies are listed 'Best First'.
Re: Re^6: Counting keys with defined or undefined elements in a hash (behaviour of values())
by Oberon (Monk) on Jun 07, 2003 at 02:34 UTC
    > I think that just throws the created scalar away, but doesn't avoid its creation.

    Well that's lame.

    > ... and requires a function call for every pair in the hash, rather than a single one for all of them.

    Right; I hadn't considered that.

    > It does use less memory though, yes, that's what I meant when I said it's desirable when dealing with large hashes.

    Ah, good, then I'm not completely insane. :-) I had switched from values to each on an ETL project last year, and I was remembering that I'd done it for speed reasons, but now that I think about it, it may very well have been for memory purposes instead.

    Thanx for the clarifications.

      Glad to help. :) As for it throwing the created scalar away - well, each gets called in list context, but it can't know what list its return value is being assigned to.

      Makeshifts last the longest.

        > ... but it can't know what list its return value is being assigned to.

        No, I suppose not. Well, not without a change to the language anyway ... I wonder if Perl 6 would be able to tell. Then again, perhaps such a detailed level of introspection isn't generally useful.