in reply to Re: setting values in anonymous hash
in thread setting values in anonymous hash

OK, I'm confused. map wants a list, right? So here you're treating the hash as a list, and setting the undefined values to '' as you find them?

I think that this method would make more sense to me if it went something like

map { $_ = '' unless defined } values %$ref2;
I guess I'm just wondering why referring to hashes in a list context is a good idea.

Update: merlyn rightly points out this is a void usage of map. Perusing the docs for map led me to some insight. We want to test for values that are undefined and change them -- sounds like a job for grep:

for (grep {not defined} values %$ref2) {$_ = q()};
fits the bill -- it actually acts on the returned values from grep, so no more void contexts, and actually reads like the task desired.

Replies are listed 'Best First'.
Re: setting values in anonymous hash
by Dominus (Parson) on Nov 28, 2000 at 22:27 UTC
    Says snax:
    > map wants a list, right?
    Right.
    > So here you're treating the hash as a list, and
    > setting the undefined values to '' as you find them?
    Right. See, you weren't as confused as you thought.
    > I guess I'm just wondering why referring to hashes
    > in a list context is a good idea.
    Uh uh. Innocent until proven guilty. It solves the problem, so the burden of proof is on you to show why it isn't a good idea.

    We use hashes as lists all the time:

    sub stuff { my %hash ; ... return %hash; } %stuff = stuff();
    It isn't a big deal.
      I had no intention of calling you guilty -- I was honestly wondering about the whole hash-in-list-context thing. I have used this "feature" myself before but long ago, before I started using -w and strict. Since then I've always felt oddly guilty using hashes like that -- that I was obfuscating, which isn't usually a good idea when you are using code for "real" purposes.

        Says snax:
        > I had no intention of calling you guilty
        Oh, I didn't mean that. I meant that you were calling the code guilty.

        Anyway, I agree with you that the use of the hash as an argument to map is a little weird, and possibly obfuscated, because it's not clear what is going to happen to the keys.