in reply to Collapsing a string to unique characters

In existing order:

perl -nle"my %seen; print grep !$seen{$_}++, /./g"

In lexical order:

perl -nle"my %seen; print sort grep !$seen{$_}++, /./g"

By the way, the chomp is useless because -nl already chomps.

>perl -MO=Deparse -nle"foo()" BEGIN { $/ = "\n"; $\ = "\n"; } LINE: while (defined($_ = <ARGV>)) { chomp $_; foo(); } -e syntax OK

Update: Oops, I had my tests inverted. Fixed.

Replies are listed 'Best First'.
Re^2: Collapsing a string to unique characters
by dwhite20899 (Friar) on Jan 09, 2009 at 14:17 UTC
    ikegami,

    I tried the lexical order method, and it's almost what I need...

    On a mac, I get this output:

    FMMNllltuwwxxxz 13AEITfhlllstxx /39CMMNhhlllllluuwwxxx 8ELMMfhllllxxx 49MMdhllllouxx 2239MMlllquxxxx 018AMMPPQTbbffhllllx /2559Mllrtuuwwxz

    If I change $seen{$_}++ to $seen{$_}+=2 then I get this:

    /034589BDFFHKMMMNNUXabcfghjllllnqsttuuwwwxxxxyzz /112334589AACEEGIIKLMNPQRSTTbdeffhhjkllllnopqssttuxxx //01335899ABCCIMMMNNRTVYbcdefghhhjkllllllloqtuuuwwwxxxxy /035889BEEFKLLMMMNOPQTabcffghhlllllmqrtuvwxxxx +/134456899ACGKMMMNOQWbddghhlllllmnooqrtuuwxxx /2223345899ABDEFGHIMMMNORTabdfhillllnqqtuuwxxxxxyz +/00113457889AACEIMMMNPPPQQTTZabbbcefffghhlllllmqtuwxx //2234555899ACGILMMNOQUbehlllmoqrrsttuuuwwwxxyzz
    which DOES list all the chars used, but has duplicates.
Re^2: Collapsing a string to unique characters
by dwhite20899 (Friar) on Jan 09, 2009 at 14:21 UTC
    *BRILLIANT* That bang did it. Sweet!