in reply to quandery about a grep statement

Perhaps I fail to understand exactly what you are doing, but doesn't this accomplish your goal?

sub beautify { my %seen; return sort {$a<=>$b} grep {!$seen{$_}++} @_; }

Replies are listed 'Best First'.
Re: Re: quandery about a grep statement
by jynx (Priest) on Mar 29, 2001 at 05:28 UTC

    Well, that's definitely a better way. The thing that annoys me is that i know that way, i've done it before. It didn't seem to work for some reason when i originally tried it (even though it does now; thanks Murphy ;-)

    thanks much,
    jynx

    Update:ok, after everyone's replies i finally realized what happened. In a coding frenzy i wanted to solve one problem and when i switched to a different technique i didn't drop some of the old artifacts (oops).

    Basically, i'm trying to keep track of whether or not a value is acceptable for grep without having to declare the arrays(this was for an obfuscation-only purpose). The fix was to declare the arrays, because of the reasons mentioned and because otherwise nothing would get passed through grep the first time through...

    My main problem is that i check for $new{$_}+1, i should be checking for $new{$_}-1.

    If anyone's interested, the resulting (admittedly horrible) code is:

    sub beautify { %old = %new; sort { $a <=> $b } grep { $new{$_}++; $old{$_}++ unless exists $old{$_}; ($new{$_} == $old{$_}) ? 1 : ($new{$_}-1 == $old{$_}) ? 1 : 0; } @_; }
    This still declares the arrays, just not the same way. Oh well, nothing's perfect, it has lumps in it ;-)

    As also noted, this is *not* the best way to do this, for normal projects of course one would use strict; and warnings.

    Thanks for the help,
    jynx

      Well, if you are doing an obfus then you may wanna blow off declaring hash arrays and use ones that are floating around and don't cause errors. See the three examples below...
      perl -we 'use strict; %_ = %ENV; print $_{TERM},$/' perl -we 'use strict; %@ = %ENV; print $@{USERNAME},$/' perl -we 'use strict; %% = %ENV; print $%{HOME},$/'

      Yes, I'm ashamed that I know about those. =) Also, just for fun, try this one on:

      perl -we 'use strict; %$ = %ENV; print ${$}{SHELL},$/' # note the extra {} on this one to keep the parser from # making the normally correct left turn of ${${SHELL}} and # crying that $SHELL isn't declared...

      --
      $you = new YOU;
      honk() if $you->love(perl)