in reply to Unique Character

I know you stated the correct value for the "character count" attribute with the text sample you provided is nineteen, which is what people would usually refer to as the length of the string.

Another characteristic is how many characters are used one or more times. I would call this the number of unique characters, but you use the term "unique" for a different purpose (see below), and it appears that this isn't a characteristic you're supposed to be deriving.

Then there's the number of characters that occur only once. You call that unique. I might consider that non-repeated characters. But it doesn't really matter what we call things as long as we understand each other. I'm going to understand that you need a total of how many characters the string contains (its length), and a count of how many characters occur exactly one time within the string (your 'unique' attribute).

In Perl, the easiest way to derive a string's length is with the length function. But if you're looking for another way, yours works, though it's probably better suited for obfuscation, and I'm sort of surprised to see it show up in a solution for a starting course. Has your course really gotten into references already?

To satisfy your definition of "unique characters", iterate over every character in the string, and increment a hash element with that character as a key. Then look at the keys of your hash, and the values they index. Anytime you come across a value of '1', you know that letter appeared only once, and you can increment a "unique chars" counter.

I'm still a little concerned about your definition of unique characters. I wonder if you've misunderstood, and really need to count how many characters appear at least one time. That's actually a little easier: Use the hash approach, but instead of only counting keys that index a value of exactly one, just tally up how many keys you have.

The second part, whether it's "characters that appear exactly once", or "characters that appear one or more times", you will find keys useful. Also look over perlfaq4, "How can I get the unique keys from two hashes, just as an example of using hashes to reduce lists of items to unique components.


Dave