Similar questions have been asked quite a few times. (More bluntly, Try a [id://Super Search|little research] first.).



One way (probably the best) is to use the uniq function from List::MoreUtils. This is used something like this:

use List::MoreUtils;
use strict;
use warnings;
my @string = qw(a a a a b b b c d e f q q q r r r);
my @singletons = uniq(@string);

> where @singletons is a list of the elements in @string which occur only once. Note, however, that this operates on lists, not strings, so you'll have to split the string.

One A second way (guaranteed to be non-optimal &grin;) is to split the string into an array and count the occurences of each character.

use strict;use warnings; # because it's a really good idea my @string = split(//, 'aabbccddeeffgghhi'); my %count; foreach(@string) { $count{$_} ++; } foreach (keys(%count)) { print "$_ is unique\n" if $count{$_} == 1; }

I'm quite sure some of the regex gurus can do it with a single regex; I tend more to the "brute force and ignorance"™ school of programming than the "subtle and brilliant" school.


added in another update

As Hofmator pointed out, I screwed up: uniq doesn't return a list of elements that occur once in the original list; it returns a list with duplicates stripped out. That's why I've struck out a much of this post.

added in update

I knew that somebody would find a one-line solution.

emc

Experience is a hard teacher because she gives the test first, the lesson afterwards.

Vernon Sanders Law

In reply to Re: Unique character in a string? by swampyankee
in thread Unique character in a string? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.