Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: number of unique characters in a string

by Limbic~Region (Chancellor)
on Mar 01, 2003 at 03:54 UTC ( [id://239636]=note: print w/replies, xml ) Need Help??


in reply to number of unique characters in a string

Here is OWTDI - but I bet by the time I finish this post, some other monk will come up with a faster, more Perlish way.

#!/usr/bin/perl -w use strict; print UniqueCount("1234567812") . "\n"; sub UniqueCount { my $string = shift; if ($string) { my %unique; return grep { !$unique{$_}++ } split //, $string; } else { return 0; } }

You will want to modify if you do not want to count spaces or if you want to be case insensitive

Hope this helps - happy hacking - L~R

Update:Dropped un-needed @array in grep

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-25 12:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found