Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Collapsing a string to unique characters

by ikegami (Patriarch)
on Jan 09, 2009 at 14:02 UTC ( [id://735182]=note: print w/replies, xml ) Need Help??


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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://735182]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2026-02-10 21:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.