in reply to state preserving uniq

Don't mean to be rude, but this can be done a lot simpler:
perl -ne '$s{lc $_}++ or print'
(It's actually a standard example to which I added lc, and unlike your version it prints out the line right at its first occurrance, rather than first slurping in the whole file)

Replies are listed 'Best First'.
Re^2: state preserving uniq
by Aristotle (Chancellor) on Mar 01, 2003 at 17:28 UTC
    Or even:
    $s{+lc}++||print
    :-)

    Makeshifts last the longest.

      Yea ofcourse; I was aiming for simplicity though, I wasn't golfing. :-)

      (it yields the same op-tree btw)

Re: Re: state preserving uniq
by Chady (Priest) on Mar 01, 2003 at 12:16 UTC
    yea, but my code use a lot more memory than yours. ;)

    typical example on how NOT to code in perl. (I should get more sleep)

    nice one-liner
    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/
Re: Re: state preserving uniq
by Anonymous Monk on Mar 01, 2003 at 15:58 UTC
    And that can be sped up even more...
    perl -ne 'print if 1 == ++$s{lc $_}'
    (The difference is that you don't have to create temporary variables.)
      Although I can't test it right now, I sincerely doubt that your version is faster, especially if you compare the optrees:

      While your version does save copying the old integer value to a temporary (you realize the temporary sv is allocated at compile-time, right?), it takes two extra ops, which I'm pretty sure costs more cpu time.

      And in any case the difference is too minimal (especially compared to lowercasing the string and doing a hash lookup) to justify adding to the code complexity