Interesting problem. Out of curiousity, I played around with the idea and got the following (ugly!) one-liner to work (reformatted for readability):

$ perl -le ' my @arry = qw/ a b cd gefge hhi jjkk l m nn o /; print q{Originals: }, join q{ }, @arry; my @n_arry = grep { if ( $_ !~ m/(.).*?\g-1/ ) { $_; } } @arry; print q{Uniques: }, join q{ }, @n_arry; ' Originals: a b cd gefeg hhi jjk l m nn o Uniques: a b cd l m o $

It can probably be cleaned up or made shorter (as I was just trying to get something working).

I originally approached the idea of doing a split-sort-join on the original string to get an ordered string, testing that for duplicate characters, but after getting it working I realized the grep() was the meat of the code, and the rest could be eliminated.

For anyone curious, here was my original code:

$ perl -le ' my @arry = qw/ a b cd gefge hhi jjkk l m nn o /; print q{Originals: }, join q{ }, @arry; my @n_arry = map { $_->{ori} } grep { if ( $_->{ordered} !~ m/(.).*?\g-1/gx ) { $_; } } map { my $str = my $o_str = $_; my @p = sort { $a cmp $b } split //, $o_str; $str = join q{ }, @p; { ori => $o_str, ordered => $str, }; } @arry; print q{Uniques: }, join q{ }, @n_arry; ' Originals: a b cd gefeg hhi jjk l m nn o Uniques: a b cd l m o $

Hope that helps.

Here's a shorter value for a single string:

$ perl -le ' my $str = shift; print q{Original: }, $str; print q{Unique: }, grep { if ( $_ !~ m/(.).*?\g-1/ ) { $_; }; } ( $str, ); ' foobar Original: foobar Unique: $ perl -le ' my $str = shift; print q{Original: }, $str; print q{Unique: }, grep { if ( $_ !~ m/(.).*?\g-1/ ) { $_; }; } ( $str, ); ' bar Original: bar Unique: bar

Update: (2022-08-15) - Removed unnecessary '/g' and '/x' from the regex in the grep. Removed an errant line of text that was left while editing the response. Removed READMORE tags around first code snippet. Fixed punctuation error in response text. Added shorter code examples at end.


In reply to Re: Nonrepeating characters in an RE by atcroft
in thread Nonrepeating characters in an RE by BernieC

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.