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

Hello,

Is there a function like C's strlen in perl? I have been counting strings like this:
my $string = "2002023884ks293jjksd" for ($x = 0; $string =~ /(.)/g; $x++) { ; }
Which is working out just fine. If there is not a strlen like function, is there a better way to do it?

Thanks in advance,
blax

Replies are listed 'Best First'.
Re: strlen like function in perl
by wog (Curate) on Dec 18, 2001 at 01:48 UTC

    You're looking for length.

    update: Though, if golf is what you want, I've heard y///c be used to get the length of $_. (It's one character shorter. And, IIRC, that trick is credited to Abigail.) (y///c or any of its equivilents is not reccommended for code you plan on actually using, of course.)

Re: strlen like function in perl
by andye (Curate) on Dec 18, 2001 at 01:55 UTC
Re: strlen like function in perl
by Ven'Tatsu (Deacon) on Dec 18, 2001 at 01:49 UTC
Re: strlen like function in perl
by Lucky (Scribe) on Dec 18, 2001 at 16:28 UTC
    Just another solution:
    $length=$string=~y///c
    Yet another solution:
    $length = scalar split /|/,$string