TASdvlper has asked for the wisdom of the Perl Monks concerning the following question:
Hello all,
I'm a little stuck on something. Say, I have a string:
my $var = "this is just a sample of a string, there is nothing really
+important in the scalar, just a lot of text";
I would like to, after every 10 characters, I would like to insert '\n' into the string. The only thing is, the 10th character must be a space. So, if the 10th character is not a space, it will continue counting until it finds a space, then enter a '\n'. So, in the string above, a '\n' will be entered after the word 'just'. And so on.
Thanks all.
Re: Counting characters in a string
by matthewb (Curate) on Mar 11, 2004 at 16:38 UTC
|
The problem that you are addressing (along with a load more) is excellently solved by the Text::Autoformat module.
For simpler needs, see Text::Wrap.
| [reply] |
|
If you wish to avoid any modules, you can do it this way:
#!/usr/local/bin/perl
use strict;
use warnings;
my $var = "this is just a sample of a string, there is nothing really
+important in the scalar, just a lot of text";
my $pos = 9;
while(( $pos = index($var, ' ', $pos)) > -1 ) {
substr($var, $pos, 1) = "\n";
$pos += 10;
}
print "\n$var\n";
Just remember, TIMTOWTDI.
- - arden. | [reply] [d/l] |
Re: Counting characters in a string
by rinceWind (Monsignor) on Mar 11, 2004 at 16:41 UTC
|
TASdvlper,
Your question looks remarkably similar to the one you posted here. Are you not happy with the answers you received before? Did you try any of the suggestions?
-- I'm Not Just Another Perl Hacker
| [reply] |
|
Similar question, but different. In the first question I didn't care about the nth character, in this case I do. This question is slightly more complicated.
| [reply] |
|
| [reply] |
Re: Counting characters in a string
by neniro (Priest) on Mar 11, 2004 at 16:59 UTC
|
If you don't wonna use one of the suggested modules, you can do it like this:
#!/usr/bin/perl
use strict;
use warnings;
my $var = "this is just a sample of a string, there is nothing really
+important in the scalar, just a lot of text";
my @box = split(/\b/, $var);
my @line;
while (@box) {
shrink();
}
sub shrink {
my $word = '';
while (length $word <= 10) {
last unless (@box);
$word .= shift @box;
}
push (@line, $word);
}
print join("\n", @line), "\n";
neniro | [reply] [d/l] |
Re: Counting characters in a string
by Roy Johnson (Monsignor) on Mar 11, 2004 at 17:29 UTC
|
Let's talk you through this. First of all, you want to substitute newlines for spaces:
s/\s/\n/g;
But you want the space to be preceded by at least 10 non-newlines:
s/(?<=[^\n]{10})\s/\n/g;
But this doesn't work, because the newlines we put in aren't found by the lookbehind, so we're going to have to consume and replace characters instead of using lookbehind:
s/([^\n]{10})\s/$1\n/g;
I'm still using [^\n] because I assume that if the string already has embedded newlines, you want the counter to reset.
Ok, bonus round: there is a way to do it with lookbehind, but it's ugly. We use \G to indicate where the last match left off. We want there to be at least 10 non-newlines between any new match and the old one. So:
s/(?<=(?:(?<!\G)[^\n]){10})\s/\n/g;
The lookbehind is "10 non-newlines, and be sure that none of them is immediately preceded by the previous match".
Update: Ok, re-reading the original question, we don't want to SUBSTITUTE for spaces, we want the newline to be inserted AFTER a space, and the space is (at least) the 10th character, meaning it's preceded by 9 chars. Thus:
s/([^\n]{9}\s)/$1\n/g;
(thanks to ambrus)
The PerlMonk tr/// Advocate
| [reply] [d/l] [select] |
Re: Counting characters in a string
by jbware (Chaplain) on Mar 11, 2004 at 16:57 UTC
|
Is this what you're looking for?
s/(.{10})([^\s]*?)\s/\1\2\n/g;
-jbWare | [reply] [d/l] |
|
s/(.{9}\S*\s)/$1\n/g
Note especially the use of $1 instead of \1.
Update: inserted the \s I forgot.
Update 2:As pointed to by Roy Johnson,
the \S* can be left out like this:
s/(.{9}\s)/$1\n/g
Also, I changed {10} to {9} as the space is the tenth character.
| [reply] [d/l] [select] |
Re: Counting characters in a string
by eric256 (Parson) on Mar 11, 2004 at 17:00 UTC
|
That was actualy a bit of fun there. Not sure how you wanted to handle cleaning up the spaces but here is my go at it. I used a while loop with a regex and the /g modifier. The regex is /(.{10,}?\b)/gThat should be read as find 10 or more occurances of any character followed by a word break ( you could change this to a space or \s, however it suits you, this just works pretty well). Here is the full bit, you would have to change the printing to concating into a new string but thats no challenge.
1234567890123456789
this is just
a sample
of a string
, there is
nothing really
important
in the scalar
, just a lot
| [reply] [d/l] [select] |
|
|