Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Counting Characters

by Anonymous Monk
on Mar 02, 2006 at 20:25 UTC ( #534040=perlquestion: print w/replies, xml ) Need Help??

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

Hi, this should be simple but...I need to count the number of characters set in the variable ($summary). My $summary variable takes the first 500 characters from another variable $text. Sometimes $text might be less than 500 characters so I need to count the number of characters that actually end up in the variable $summary. This is how I get the summary variable...
$summary = substr($text, 0, 500); Thanks.

Replies are listed 'Best First'.
Re: Counting Characters
by ikegami (Pope) on Mar 02, 2006 at 20:43 UTC

    Use length. Specifically, use length($summary).

    In context:

    $text = '.'x400; $summary = substr($text, 0, 500); print(length($summary), "\n"); # 400 $text = '.'x500; $summary = substr($text, 0, 500); print(length($summary), "\n"); # 500 $text = '.'x600; $summary = substr($text, 0, 500); print(length($summary), "\n"); # 500

      As an addendum -- in case it's not clear for the OP or others -- this works because substr used in the way described takes up to as many characters specified (e.g. 500 in this case). If used on a string shorter than the maximum length, there is no padding added at the end. Thus length works on the summary exactly as one would expect.

      -xdg

      Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: Counting Characters
by PodMaster (Abbot) on Mar 03, 2006 at 04:33 UTC
Re: Counting Characters
by CountZero (Bishop) on Mar 02, 2006 at 21:02 UTC
    print scalar split '', $summary; or if you really want to count the number of characters: map {$count++} split '', $summary; print $count;

    Of course none of these is efficient, but if you really do not want to use the built-in length function, ...

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      Don't forget about print $summary =~ y///c;.

      or counting nibbles and dividing by two (bit fraught for multi-byte characters though):

      use strict; use warnings; my $text = '.'x400; my $summary = substr($text, 0, 500); print length(unpack 'H*', $summary) / 2, "\n";

      Prints:

      400

      DWIM is Perl's answer to Gödel

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2021-02-26 19:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?