Finding the first non-repeated character in a string Given a string, find the first non-repeated character in it.

E.g. "yellow" should return "y"

There are several solutions for this in other languages but I haven't seen one written in Perl.

My main question is: can this be done in a more Perl-ish way or generally in an even shorter way?

use strict;
use warnings "all";

while (&lt;DATA&gt;)
{
    while (m,(.),g)
    {
        my $c = $1;
        if (s,$1,,g &lt; 2)
        {
            print "$c\n";
            last;
        }
    }
}

__DATA__
yellow
tooth
Don't reinvent the wheel, there is a function (ie. singleton) in the package MoreUtils that does the job:

#!/usr/bin/perl
use Modern::Perl;
use List::MoreUtils qw(singleton);

while (&lt;DATA&gt;) {
    chomp;    # don't forget it, it removes the linebreak.
    # split explodes the string in character
    # singleton keeps characters that appear only once
    # ($first) contains the first character that appears only once.
    my ($first) = singleton split//, $_;
    say $first;
}

__DATA__
yellow
tooth


Output:

y
h