Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Counting characters without a space

by htmanning (Friar)
on Jul 22, 2018 at 01:52 UTC ( [id://1219030]=perlquestion: print w/replies, xml ) Need Help??

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

I have a form that users are filling in by listing numbers separated by commas, but not adding a space. This results in a very long string which makes the resulting page too wide. My thought it to count the number of characters in each word and target any word more than 15 characters without a space. I would then replace the commas with a comma and space. Anyone have a better way to do it?

How do I find words that are longer than 15 characters?

Replies are listed 'Best First'.
Re: Counting characters without a space
by roboticus (Chancellor) on Jul 22, 2018 at 02:48 UTC

    htmanning:

    You can certainly do that, though it might be a bit of work. One way you could do it would be something like breaking the string into an array of strings on blanks. Then, for each string in the array that happens to be over 15 characters, you could split on the first comma. Finally, rejoining the array back into a string. Should be something (untested!) like this:

    my $reformatted = join(' ', map { length($_) < 16 ? $_ : split /,/,$_,2 } split /\s+/, $original);

    I don't particularly like that, though. If I was willing to reformat the users string, I'd just do something like:

    $original =~ s/,\s+/, /g;

    The code is simple, and it simply ensures that all commas are followed by a space.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Thanks! This should work even though I don't know what the "s+" does.

      <code> $original =~ s/,\s+/, /g; </code.

      I was considering the above without the s+.

      What is happening is they are putting in numbers like this: 1234,4325,5456,6544,4345,5443,3455,4321. Your suggestion should work.

        I don't know what the "s+" does.

        It isn't "s+" but rather "\s+" - a small but very important distinction. \s is a character class which includes all whitespace (spaces, tabs, newlines). The + metacharacter matches one or more of the preceding item so this combination matches one or more consecutive whitespaces. See perlre or the Monastery's own tutorials for more on regular expressions.

Re: Counting characters without a space
by AnomalousMonk (Archbishop) on Jul 22, 2018 at 03:43 UTC

    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:  <%-{-{-{-<

      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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-25 20:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found