On Aug 13, [EMAIL PROTECTED] said:
sub quickWrap { my $data = @_[0];
You shouldn't use an array slice where you mean to use a single array element.
Thanks for catching that, I should have really seen that one.
$times_to_reread_my_code_before_posting_to_list++;
my $data = shift; my $wrap_at = @_ ? shift : 75;
I like that.
Something tells me you're not sure what a character class does. A character class is for CHARACTERS. Therefore, you don't use | in it. The
Thanks for the correction character classes *runs to fix about half a dozen regexes*
> This regex looks familiar. I'm going to suggest a big change in a > bit. > Oh, and [\s|\S], which could be [\s\S], is kind of awkward.
what is less awkward than [\s|\S] for 'match anything?'
EWW. DON'T USE $`. It's terrible.
Okay, is that because it is slow and makes the rest of the regular expressions afterwards run slowly? (I saw something about that in the perlre document)
Is it as bad to use something like '(match anything)' before the main expression, and using $1 in place of $` when it's useful?
Ok, here's my idea: instead of matching text and putting it in a new string, why not CHANGE the string we're working on as we match it? We can do that using a substitution, the s/// operation.
That is a better approach, I had given up on that when I couldn't understand why it was failing, and thought I could follow the logic better if I broke it down into a match in a loop.
We want to match UP TO $wrap_at characters, as many as possible, and add a newline after them, SO LONG as it's in the place of a space. Here's a regex I think will do the job for you:
sub quick_wrap { # I use the word_word_word style, not wordWordWord my $str = shift; my $wrap_at = @_ ? shift : 60;
$str =~ s{(.{1,$wrap_at})\s}{$1\n}g;
return $str; }
The regex matches between 1 and $wrap_at characters (trying to match the most possible) that are followed by a space. It replaces this with the text it matched (and captured to $1) followed by a newline. Let me know if this does what you expected.
That is exactly what I was trying to do, and that's a far, far more elegant way to do it.
I think I'll review all my material again on regexes. Are there any good books you recommend on how to use and think in regexes?
Thanks again for your help, that has really really helped.
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]