in reply to Re: Character Count
in thread Character Count

Your solution will miss punctuation, whitespace, and international character sets. length is the correct solution.

----
I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer

:(){ :|:&};:

Note: All code is untested, unless otherwise stated

Replies are listed 'Best First'.
Re: Character Count
by Abigail-II (Bishop) on Oct 15, 2003 at 19:29 UTC
    length() was my first thought as well. Then I read the question, which specifies numbers and letters. So, missing out on punctuation, whitespace, etc, was exactly what I intended.

    Abigail

      From your first answer, it's not immediately apparent to those of us not familiar with the fact that the tr operator that it returns the number of matched characters.

      For those of us who read replies in the hopes of learning something, extra information like this would be very useful...especially, I think, in a case such as your first reply here.

      Sure, I spent half an hour (amidst doing other things) reading the perldoc -f tr and then perldoc perlop manpages...And I'm not likely to forget anytime soon that tr returns the number of characters matched...But somehow, the reply of tr/a-zA-Z0-9// in toto I find wanting...

        Hmm, the return value of tr/// is discussed in Learning Perl under Transliteration, in Camel under the Regexp section and the tr listing in the function description section. It is described in perlfaq4 under the heading, "How can I count the number of occurrences of a substring within a string?", and in perlfunc tr.

        How can you be upset that someone finally gave you a reason to learn something that is so fundamental that it appears in the FAQ and beginner's books? Your reaction is especially incredable considering that the tr/// solution actually directly answers the literal meaning of the question posed. Perhaps not every word in the question was to be taken literally, but that's not really something I like speculating on. Anyway, it's not like being forced to understand perlguts.


        Dave


        "If I had my life to do over again, I'd be a plumber." -- Albert Einstein
Re: Re: Re: Character Count
by revdiablo (Prior) on Oct 15, 2003 at 19:09 UTC

    This is largely a matter of interpretation. While you are probably correct that length is what the OP really wants, he did state the following:

    If a string has less than eight characters (numbers/and or letters) [emphesis added]

    Which would support the solution given by Abigail.