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

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.

Replies are listed 'Best First'.
Re: Re: setting values in anonymous hash
by snax (Hermit) on Nov 28, 2000 at 22:40 UTC
    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.

        Well, between your snippet and merlyn's, and a CB comment by tye, I've come to a much greater understanding of what's going on. I think :)

        If I'm understanding properly, you could do anything to the keys in this setting, because they're just strings and you can't get references to them to allow their modification, which is how the values get modified.

        That's what my test code suggests, anyway:

        #!/usr/local/bin/perl -w use strict; my $ref={ a => 'aa', b => 'bb'}; print "The hash, before:$/"; print %$ref; print qq($/The hash "during":$/); for (%$ref) { /[A-Z]/ or $_ = 'x'; print; } print "$/The hash, after:$/"; print %$ref; print $/; __END__ The hash, before: aaabbb The hash "during": xxxx The hash, after: axbx
        This is perl, v5.6.0, BTW.