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

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

Replies are listed 'Best First'.
Re^10: Remove redundency from an array
by ikegami (Patriarch) on Sep 24, 2007 at 20:41 UTC
    I'll give the example, then
    use Test::More tests => 2; sub uinq { @_ = do { @$_{@_}=(); keys %$_ }; @_ } my @array = (1,1,3,3,2,3,5,7,5,2); %$_ = ( f=>'oo', b=>'ar' ); is keys(%$_), 2; print "@{[ uinq @array ]}\n"; is keys(%$_), 2;
      OK, this case *would* be solved by
      a local $_ before the do block (I confess ..)

      use strict; use warnings; use Test::More tests => 2; sub uinq { local $_; @_ = do { @$_{@_}=(); keys %$_ } } my @array = (1,1,3,3,2,3,5,7,5,2); %$_ = ( f=>'oo', b=>'ar' ); is keys(%$_), 2; print "@{[ uinq @array ]}\n"; is keys(%$_), 2;

      That correction seems to be all what's needed here.
      Thanks for clearing this up
      But now it's late ...

      Regards
      mwa

        That still modifies a global variable ...or ast least, it's suppose to. I found that it doesn't modify %{''} as it should! Your code is excercising a Perl bug! This gets better all the time.

        Again, easily solved by replacing
        sub uinq { local $_; @_ = do { @$_{@_}=(); keys %$_ } }
        with the simpler, safer
        sub uinq { local %_; @_{@_}=(); keys %_ }

        What's with the useless do?
        What's with the useless assignment to @_?
        Why use a reference when one isn't needed?
        Why use a symbolic reference when one isn't needed?
        Using the symbolic reference means there's now another variable to localize.
        That is, if the code didn't rely on a Perl bug.