Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Counting characters in a string

by TASdvlper (Monk)
on Feb 06, 2004 at 14:34 UTC ( [id://327079]=perlquestion: print w/replies, xml ) Need Help??

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

All,

I've read some of the threads here on counting characters in a string, and I got some good information, but not quite all the answers I was looking for. Say I have a string:

my $string = "this is just a test\nthis is another test\nthis is yet a +nother test\n";
What I want to do is print n-amount of characters then start a new line. So, say n = 5 (where n is actually a variable). The output would be:
this(space) is ju st a t est\nt this(space) is an ... an so on ...
any thoughts on this ???

Replies are listed 'Best First'.
Re: Counting characters in a string
by duff (Parson) on Feb 06, 2004 at 14:41 UTC
    my $str = "this is just a test\nthis is another test\nthis is yet anot +her test\n"; $str =~ s/(.{5})/$1\n/sg; print $str;

    But that actually outputs:

    ---start output--- this is ju st a test this is an other test this is y et an other test ---end output---

    Which isn't what your example showed, but is what you asked for.

Re: Counting characters in a string
by Sol-Invictus (Scribe) on Feb 06, 2004 at 15:15 UTC
    In other words you want to wrap your string ?

    #!perl-w use Text::Wrap; $string = "this is just a test\nthis is another test\nthis is yet a no +ther test\n"; $Text::Wrap::columns = 4; print wrap('', '', $string);

    Sol-Invictus

    You spend twenty years learning the spell that makes nude virgins appear in your bedroom, and then you're so poisoned by quicksilver fumes and half-blind from reading old grimoires that you can't remember what happens next.

Re: Counting characters in a string
by Roy Johnson (Monsignor) on Feb 06, 2004 at 14:39 UTC
    my $c_per_line = 5; print "$1\n" while ($string =~ /(.{0,$c_per_line})/sg);
    Update: added "0," so that the match would include the final few characters.

    The PerlMonk tr/// Advocate
Re: Counting characters in a string
by valentin (Abbot) on Feb 06, 2004 at 14:40 UTC
    print map { "$_\n" } $string =~ /.{1,5}/g;
      That will stop at every newline and continue again after it:
      $ perl -we'$string = "abcdef\nghijk\nlmno\npqr\nst\nu"; > print map { "$_\n" } $string =~ /.{1,5}/g;' abcde f ghijk lmno pqr st u
      You either want the //s flag on the match or some other way of dealing with newlines.
        thanks, you are right //s is fine for me.
Re: Counting characters in a string
by duff (Parson) on Feb 06, 2004 at 15:06 UTC

    Here's another method if you're running perl 5.8.0 or better:

    $str = "this is just a test\nthis is another test\nthis is yet another + test\n"; open F, "<", \$str or die; # open a handle to our string $/ = \5; # Read 5 characters at a time while (<F>) { # Read 'em! print "$_\n"; }
Re: Counting characters in a string
by arden (Curate) on Feb 06, 2004 at 14:43 UTC
    Wow, this smells like homework. :)
    Anyway, you could split the string into an array of single characteres and print off each element within a loop adding a "\n" every five characters.
    You could use substr and print five character slices plus a "\n" until you get to the end of the string.
    You could make a format, but that's not as fun IMHO.

        So, what have you tried so far TASdvlper?

Re: Counting characters in a string
by ysth (Canon) on Feb 06, 2004 at 16:28 UTC
    I'm not sure what you are expecting when you have a newline in the input. From your example, it would seem that you want to put a backslash and 'n' in the output, or perhaps to put a newline in the output and have it count as perhaps either one or two characters.

    If what you want is the former, this is how I would do it (it will escape all other non-printable characters as well).

    $ perl -w my $string = "this is just a test\nthis is another test\nthis is yet a +nother test\n"; use Data::Dumper; { local $Data::Dumper::Terse = 1; local $Data::Dumper::Useqq = 1; local $, = "\n"; print substr(Dumper($string),1,-2)=~/.{1,5}/gs; } __END__ this is ju st a test\ nthis is a nothe r tes t\nth is is yet anoth er te st\n
Golf?: Counting characters in a string
by mr_mischief (Monsignor) on Feb 06, 2004 at 16:01 UTC
    perl -pe'$_=join"\n",/.{0,5}/g'

    Update 20040205 @ 1020 US/CST: or

    perl -pe's/(.{1,5})/$1\n/g'

    which is even shorter...



    Christopher E. Stith
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
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://327079]
Approved by DaWolf
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-04-23 16:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found