Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Character Count

by natty_dread (Acolyte)
on Oct 15, 2003 at 13:38 UTC ( [id://299411]=perlquestion: print w/replies, xml ) Need Help??

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.

Log In?
Username:
Password:

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

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

    No recent polls found