Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Counting number of characters in a string

by jeffpflueger (Beadle)
on Mar 13, 2001 at 01:12 UTC ( #63955=perlquestion: print w/ replies, xml ) Need Help??
jeffpflueger has asked for the wisdom of the Perl Monks concerning the following question:

Any quick tricks anyone has to simply counting the number of characters in a string? (I can think of some long ways to do this, but there must be a quicky)

Jeff Pflueger - Struggling Perl knowledge sponge

Comment on Counting number of characters in a string
Re: Counting number of characters in a string
by chromatic (Archbishop) on Mar 13, 2001 at 01:13 UTC
      the shortest ones are the best
      Rudif
        This does not work for me. I expect the length of the string 'abc' to be 3, but your code returns 0:
        use strict; use warnings; my $str = 'abc'; my $cnt = @{[$str =~ /(\.)/g]}; print "$cnt\n"; __END__ 0
      hi all, counting number of characters is so easy.. use:length($stringname); $x=length($stringname);
Re: Counting number of characters in a string
by japhy (Canon) on Mar 13, 2001 at 01:22 UTC
Re: Counting number of characters in a string
by Anonymous Monk on Mar 13, 2001 at 01:49 UTC
    @arrayToReceiveChars = split(//,$varHoldingString); $len = @arrayToReceiveChars; print $len;
      if you're not in a subroutine, doing a split in scalar context: $length=split(//, $string) acheives the same thing. Don't do this in a subroutine though, or else you'll clobber your @_.
Re (tilly) 1: Counting number of characters in a string
by tilly (Archbishop) on Mar 13, 2001 at 02:29 UTC
(tye)Re: Counting number of characters in a string
by tye (Cardinal) on Mar 13, 2001 at 02:35 UTC
    my $count= 0; for(0..255){ my $c= pack "C", $_; $count += () = $str =~ /\Q$c\E/g; }

    Warning! This probably yields an inaccurate count for Unicode strings.

            - tye (but my friends call me "Tye")

      I just realized that most strings won't have most characters in them so this can be sped up significantly via:

      my $count= 0; for( keys %{ { map {$_,1} unpack "C*", $str } } ){ my $c= pack "C", $_; $count += () = $str =~ /\Q$c\E/g; }
              - tye (but my friends call me "Tye")
Re: Counting number of characters in a string
by Falkkin (Chaplain) on Mar 13, 2001 at 02:38 UTC
Re: Counting number of characters in a string
by OeufMayo (Curate) on Mar 13, 2001 at 02:40 UTC
(jcwren) Re: Counting number of characters in a string
by jcwren (Prior) on Mar 13, 2001 at 02:42 UTC
    #!/usr/local/bin/perl -w use strict; { my $string = "Oh please, good sir program, what is my length?"; printf ("The length is %d characters\n", howlong ($string)); use Inline C => <<'END_OF_C_CODE'; #include <string.h> int howlong(char *s) { return strlen (s); } END_OF_C_CODE }
    --Chris

    e-mail jcwren

      What do you have against nul bytes? (:

              - tye (but my friends call me "Tye")

      It didn't working

      use strict; { my $string = "Oh please, good sir program, what is my length?"; printf ("The length is %d characters\n", howlong ($string)); use Inline C => <<'END_OF_C_CODE'; #include <string.h> int howlong(char *s) { return strlen (s); } END_OF_C_CODE }

        Works fine on Linux. I imagine you're missing either Inline::C or string.h and the associated library.

Re (tilly - silly) 1: Counting number of characters in a string
by tilly (Archbishop) on Mar 13, 2001 at 02:48 UTC
    { $str =~ s/./\n/g; local $/ = ""; $len = chomp($str); }
Re: Counting number of characters in a string
by t0mas (Priest) on Mar 13, 2001 at 12:51 UTC
    Yet Another Silly Way Of Counting Characters In A String (YASWOCCIAS):
    $_="YASWOCCIAS"; s/./+1/g; print eval;


    /brother t0mas
      Shouldn't that be s/./+1/gs;
        $_="YASWOCCIAS"; don't contain any newlines so it doesn't matter in this case. In other cases it might, depending on how you define "character" in your application. For some apps (like word processors), s/\S/+1/g; might be the right choise if you like to use this algorithm in your production environment... :-)

        /brother t0mas
Re: Counting number of characters in a string
by dfog (Scribe) on Mar 14, 2001 at 01:05 UTC
    my $String = "This is the way I do it"; my @Chars = split("", $String); print ++$#Chars;
Re: Counting number of characters in a string
by archon (Monk) on Mar 14, 2001 at 01:44 UTC
    sicker and twisteder!!
    use strict; use IPC::Open2; my $pid = open2 (\*RDRFH, \*WTRFH, '/usr/bin/wc', '-c') or die "Couldn't open pipe to wc: $!"; if ($pid) { my $string = "shoes and ships and sealing wax"; print WTRFH $string; close (WTRFH); my $len = <RDRFH>; $len =~ s/^\s+//; print "length: $len"; close (RDRFH); }
Re: Counting number of characters in a string
by petral (Curate) on Mar 15, 2001 at 01:30 UTC
    Count the dots using a regular expression:
    $str="There's many more ways to do it.\n"; $str =~ tr/\000-\xff/./; while ( $str =~ /(.)/g ) { $count++; }
    You can check this using a different method:
    $count2++ while chop $str; $count eq $count2 or warn "there's something wrong with this code.\n";


    p

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2014-10-22 00:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (112 votes), past polls