Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

How do I count characters

by Anonymous Monk
on Apr 21, 2002 at 08:34 UTC ( #160855=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I am trying to write a script that counts each character (letters, numbers, spaces, line breaks, etc.) in a string. Then, after I have that information, I would like to divide the string up into new, smaller strings. For example, if the initial string $original has 1,553 total characters, I would like string $part1 to have the first 500 characters of $original, $part2 to have the second 500, $part3 to have the third 500 and string $part4 to have the remaining 53 characters (no portion can exceed the 500 character count). If this makes sence to anyone, can anyone please help? Thanks, Tony P.S. If you have any idea on how to make sure the script does not separate words such that one part of the word is in one string and the remaining part is in another, I would really appreciate if you told me how to do it.

Replies are listed 'Best First'.
Re: How do I count characters
by davorg (Chancellor) on Apr 21, 2002 at 08:54 UTC

    You count the characters in a string use length. To split a string into substrings use substr.

    But to take a piece of text and wrap it into lines of a certain length, use the Text::Wrap module.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

(cLive ;-) Re: How do I count characters
by cLive ;-) (Prior) on Apr 21, 2002 at 13:59 UTC
    If you can't use davorgs advice above, you can use this:
    my $string = "This is a test string containing fifty-four characters"; # for example, we'll set limit at 25 my $limit = 25; my @lines = (); push @lines, $_ for ($string =~ /\s*(.{0,$limit}\b)gs); # regex explanation \s* - match any leading spaces ( - start match . - match any character {0,$limit} - up to $limit times ) - end match \b - followed by a word boundary # modifiers g - global (match as many times as you can) s - treat whole string as a single line.

    hth

    cLive ;-)

      You could of course embed the comments into the regex itself, with the /x modifier:

      / \s* # Leading whitespace ( # Save match group . # Any character {0,$limit} # Up to $limit times ) # End of match group \b # Word boundary /gsx # Global, single line, extended

      - Yes, I reinvent wheels.
      - Spam: Visit eurotraQ.
      

Re: How do I count characters
by Anonymous Monk on Apr 21, 2002 at 11:51 UTC

    There are functions in perl that tell you the number of characters in a string. You could use the "length" function, for example:

    $string="fart"; $x=length($string);

    This would set the value of $x to 4.

    As far as recognizing words are concerned, you could use the "split" function to separate the string you're working on into elements of an array in which each element consists of 1 word. For example:

    $string="I've just farted"; @stringarray=split(' ',$string);

    In the above code, the elemets of the "@stringarray" array were set as follows:

    $stringarray[0]="I've" $stringarray[1]="just" $stringarray[2]="farted"

    So "@stringarray" actually contains the whole sentence separated into words.

    2002-04-21 Edit by Corion : Added formatting

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2021-02-26 18:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?