in reply to Counting characters without a space

I don't fully understand your requirements (no example data, no expected output), but it seems as if you might want to insert a newline at every place in a long line where the next number and comma would cause the line to exceed n columns. Maybe something like this (needs Perl 5.10+ for  \K regex operator, but this can easily be worked around):

c:\@Work\Perl\monks>perl -wMstrict -le "use 5.010; ;; my $s = '1,12,123,1234,12345,123456,1234567,12345678,123456789,1234567890'; $s x= 11; ;; my $max_cols = 29; my $max = $max_cols - 1; ;; $s =~ s{ \G .{1,$max} , \K } {\n}xmsg; print ' 1 2 3 4'; print '1234567890123456789012345678901234567890'; print $s; " 1 2 3 4 1234567890123456789012345678901234567890 1,12,123,1234,12345,123456, 1234567,12345678,123456789, 12345678901,12,123,1234, 12345,123456,1234567, 12345678,123456789, 12345678901,12,123,1234, 12345,123456,1234567, 12345678,123456789, 12345678901,12,123,1234, 12345,123456,1234567, 12345678,123456789, 12345678901,12,123,1234, 12345,123456,1234567, 12345678,123456789, 12345678901,12,123,1234, 12345,123456,1234567, 12345678,123456789, 12345678901,12,123,1234, 12345,123456,1234567, 12345678,123456789, 12345678901,12,123,1234, 12345,123456,1234567, 12345678,123456789, 12345678901,12,123,1234, 12345,123456,1234567, 12345678,123456789, 12345678901,12,123,1234, 12345,123456,1234567, 12345678,123456789, 12345678901,12,123,1234, 12345,123456,1234567, 12345678,123456789, 1234567890
Try various values of  $max_cols and see how it looks. See also Text::Wrap.


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: Counting characters without a space
by Cristoforo (Curate) on Jul 22, 2018 at 22:34 UTC
    Just to illustrate how this might be done using Text::Wrap.
    #!/usr/bin/perl use strict; use warnings; use Text::Wrap; my $s = '1234,4325,545678910,6544,4345,5443,3455,4321'; $Text::Wrap::columns = 15; $Text::Wrap::break = ','; # use this line if you need trailing commas at the end of each line #$Text::Wrap::separator = ",\n"; print wrap('', '', $s); __END__ C:\Old_Data\perlp>perl text_wrap2.pl 1234,4325 545678910,6544 4345,5443,3455 4321
    A reply falls below the community's threshold of quality. You may see it by logging in.