How do I remove duplicate characters and keep the unique one only in Perl?

前端 未结 11 758
隐瞒了意图╮
隐瞒了意图╮ 2020-12-05 16:08

How do I remove duplicate characters and keep the unique one only. For example, my input is:

EFUAHUU
UUUEUUUUH
UJUJHHACDEFUCU

Expected out

11条回答
  •  鱼传尺愫
    2020-12-05 16:24

    If the set of characters that can be encountered is restricted, e.g. only letters, then the easiest solution will be with tr
    perl -p -e 'tr/a-zA-Z/a-zA-Z/s'
    It will replace all the letters by themselves, leaving other characters unaffected and /s modifier will squeeze repeated occurrences of the same character (after replacement), thus removing duplicates

    Me bad - it removes only adjoining appearances. Disregard

提交回复
热议问题