Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Unique Character

by Kenosis (Priest)
on Feb 01, 2014 at 19:31 UTC ( [id://1072986]=note: print w/replies, xml ) Need Help??


in reply to Unique Character

Kudos to you for being up front about this being a school assignment!

Will provide the mechanism that's typically used for this, so you can work it out...

In such cases, a hash is usually used to tally unique characters/words/phrases, as keys of the hash. It's important to convert the characters to all upper- or lower-case, before using them as a key.

For example, converting the characters of "Perl Monks Rocks!!!" to all upper-case, your hash (e.g., %hash), could contain (could because of the algorithm you finally choose) the following key/value pair:

'O' => 2

Note that "O" has a count of 2. Getting the number of the hash's keys will give you a count of the unique characters--provided they're all the same case. Summing all the hash's values will give you the total number of characters.

Now, having said all this, how is it that the string in question has only three unique characters? What is the character property used such that only three would be considered unique? If it's being upper-case, you can still use a hash (e.g., with the keys "upper" and "other"), but with a regex that would distinguish between upper-case and all other characters.

A separate item: always, at the top of your scripts:

use strict; use warnings;

Doing so will likely save you many headaches...

Hope this helps!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2024-04-18 06:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found