in reply to Re: Re: Better way to search?
in thread Better way to search?

It's a hash slice.

It fills the hash %exts with the contents of your @exts array. The keys are set to the values in @exts, and the values in the hash are set to undef. That's why you need to test for exists rather than "truth"; because the values aren't "true", but the keys do exist.

Replies are listed 'Best First'.
Re: Re: Re: Re: Better way to search?
by r.joseph (Hermit) on Jan 05, 2001 at 06:23 UTC
    Ah, I see. So if I have this:
    @exts = {'jpg','jpeg','gif'); %exts; @exts{@exts} = ();
    then we get an %exts array that is the same as this:
    %exts = ('jpg'=>'','jpeg'=>'','gif'=>'');
    Right? Why do you so @exts{@exts} and not %exts{@exts}?
      Right? Why do you so @exts{@exts} and not %exts{@exts}?
      Because one's legal and the other isn't!

      A slice acts as an array. So therefore, the starting symbol is "at", meaning "array".

      -- Randal L. Schwartz, Perl hacker

        Slices don't act like arrays in scalar context though.

        That has always bugged me. Personally I wish that Perl were a little less DWIM about:

        $elem = @array[0];
        If that did something completely unexpected to the novice, then they might learn sooner what the correct notation is...
        Sorry to keep bugging y'all, but this is really helpful and interesting. So if I did something like this:
        @exts{@exts} = @exts;
        would that make both the keys AND the values equal (and obviously, the values of the @exts array)?
      Regarding the first question: not quite, s/''/undef/g, and then they're the same.