natty_dread has asked for the wisdom of the Perl Monks concerning the following question:

Hello Brethren.... I'm trying to count the number of characters in a string within an if statement, I haven't been able to find any info on it.
The logic is this........

If a string has less than eight characters (numbers/and or letters)

* Do this.

Sorry no code....no clue. Jah lives.

Replies are listed 'Best First'.
Re: Character Count
by broquaint (Abbot) on Oct 15, 2003 at 13:45 UTC
    You want the length function e.g
    perl -le 'my $str = "perlmonks"; print "yep" if length $str == 9' yep
    HTH

    _________
    broquaint

Re: Character Count
by danger (Priest) on Oct 15, 2003 at 13:45 UTC

    The perlfunc manpage has a listing of categorized built-in functions which can be very helpful when one is just learning the language. The following should allow you to read it on your own system (if perl was properly installed):

    perldoc perlfunc

    Looking in the category for 'scalars and strings' we see a length function. Use perldoc again to see if that will do what desire:

    perldoc -f length
Re: Character Count
by Abigail-II (Bishop) on Oct 15, 2003 at 13:46 UTC
    tr/a-zA-Z0-9//

    Abigail

      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

        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

        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.