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

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}?

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Better way to search?
by merlyn (Sage) on Jan 05, 2001 at 06:31 UTC
    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)?
        Yes. See:
        my @exts = qw/foo bar baz/; my %exts; @exts{@exts} = @exts; use Data::Dumper; print Dumper \%exts;
        produces:
        $VAR1 = { 'foo' => 'foo', 'baz' => 'baz', 'bar' => 'bar' };
        I might point out that nothing answers a question so well as testing a bit of code out yourself. :)
Re: Re: Re: Re: Re: Better way to search?
by runrig (Abbot) on Jan 05, 2001 at 10:28 UTC
    Regarding the first question: not quite, s/''/undef/g, and then they're the same.