in reply to Re^5: Remove redundency from an array
in thread Remove redundency from an array

Did you test this with the sub{}-example I provided in order to show context dependency?

Nope. I know it does exactly what you said it does. What it doesn't do is show what jdporter's solution has a problem. That would surely involve using jdporter's solution.

where would preservation of %_ (jdporter mentioned $_) usually be of some importance?

jdporter mentioned *_, not $_.

And localizing %_ would be "of some importance" when %_ is changed, like in the parent of the post to which jdporter replied. The same applies to all global variables.

Replies are listed 'Best First'.
Re^7: Remove redundency from an array
by mwah (Hermit) on Sep 24, 2007 at 20:06 UTC
    ikegami:  I know it does exactly what you said it does

    Then I'll be glad to give some enlightment to you:
    sub uinq { @_ = do { @$_{@_}=(); keys %$_ }; @_ } my @array = (1,1,3,3,2,3,5,7,5,2); %_ = ( f=>'oo', b=>'ar' ); print "@{[ uinq @array ]}\n"; print map "\t$_ => $_{$_}\n", keys %_;

    ikegami:  jdporter mentioned *_, not $_.

    Better read back again then: http://perlmonks.org/?node_id=640777

    Regards & thanks
    mwa

      Oh, I see, sorry.

      Actually, both jdporter and I got it wrong.
      You don't modify $_ like jdporter thought.
      You don't modify %_ like I thought.

      Wow, that piece of code is so incredibly bad! You modify some random variable based on the current contents of $_. Whether it'll work or not is random. Most of the time, it won't.

      for ( [qw(a a b c)], [qw(d e e f)], ) { print(uinq(@$_), "\n"); # Can't coerce array into hash }
      for ('a a b c', 'd e e f') { print(uinq(split), "\n"); # Can't use string ("a a b c") as a HASH + ref while "strict refs" in use }

      Adding local $_; causes the a specific variable to be used. That alieviates some of the problem, but that variable needs to be localized too. Why not just use %_?

      As an expression:

      do { local %_; @_{@_}=(); keys %_ }

      As a sub:

      sub uinq { local %_; @_{@_}=(); keys %_ }
        ikekami: Wow, that piece of code is so incredibly bad! ...

        Ok, ok, this is my first day on P.M. where I played around
        some time longer and tried to get used to the viewing habits of
        the holy men.

        So I learned I can't just give a simple
        example in order to state what's meant - no, one has to treat
        each code block by Test::Harness several times in each context
        available before posting - to avoid destructive criticism.

        So be it. Thanks for introducing me to P.M.


        Regards & good night (its past 22:00 in Europe ;-)

        mwa