Re: Regexp to remove spaces

2009-12-29 Thread Dr.Ruud
sftriman wrote: Dr.Ruud: sub trim { ... }#trim You're missing the tr to squash space down To trim() is to remove from head and tail only. Just use it as an example to build a "trim_and_normalize()". So I think it can boil down to: sub fixsp7 { s#\A\s+##, s#\s+\z##, tr/ \t\n\r\f/ /

Re: Regexp to remove spaces

2009-12-28 Thread Shawn H Corey
sftriman wrote: > So I think it can boil down to: > > sub fixsp7 { > s#\A\s+##, s#\s+\z##, tr/ \t\n\r\f/ /s foreach @_; > return; > } sub fixsp7 { tr/ \t\n\r\f/ /s, s#\A\s##, s#\s\z## foreach @_; return; } Placing the tr/// first reduces the number of characters scanned for s#\s\z## which m

Re: Regexp to remove spaces

2009-12-28 Thread sftriman
On Dec 23, 2:31 am, rvtol+use...@isolution.nl (Dr.Ruud) wrote: > sftriman wrote: > > 1ST PLACE - THE WINNER:  5.0s average on 5 runs > > > # Limitation - pointer > > sub fixsp5 { > > ${$_[0]}=~tr/ \t\n\r\f/ /s; > > ${$_[0]}=~s/\A //; > > ${$_[0]}=~s/ \z//; > > } > > Just decide to change in-place,

Re: Regexp to remove spaces

2009-12-23 Thread Dr.Ruud
sftriman wrote: 1ST PLACE - THE WINNER: 5.0s average on 5 runs # Limitation - pointer sub fixsp5 { ${$_[0]}=~tr/ \t\n\r\f/ /s; ${$_[0]}=~s/\A //; ${$_[0]}=~s/ \z//; } Just decide to change in-place, based on the defined-ness of wantarray. sub trim { no warnings 'uninitialized'; if

Re: Regexp to remove spaces

2009-12-22 Thread sftriman
Thanks to everyone for their input! So I've tried out many of the methods, first making sure that each works as I intended it. Which is, I'm not concerned with multi-line text, just single line data. That said, I have noted that I should use \A and \z in general over ^ and $. I wrote a 176 byte

Re: Regexp to remove spaces

2009-12-21 Thread Jim Gibson
At 6:11 PM +0800 12/21/09, Albert Q wrote: 2009/12/20 Dr.Ruud > > For a multi-line buffer you can do it like this: perl -wle ' my $x = <<"EOT"; 123456 \t abc def \t\t\t\t\t\t\t\t *** *** *** \t EOT s/^\s+//mg, s/\s+$//mg, s/[^\S\n]+/ /g for $x; I kno

Re: Regexp to remove spaces

2009-12-21 Thread Albert Q
2009/12/20 Dr.Ruud > > sftriman wrote: > >> I use this series of regexp all over the place to clean up lines of >> text: >> >> $x=~s/^\s+//g; >> $x=~s/\s+$//g; >> $x=~s/\s+/ /g; >> >> in that order, and note the final one replace \s+ with a single space. >> > > The g-modifier on the first 2 is bog

Re: Regexp to remove spaces

2009-12-21 Thread Dr.Ruud
Shawn H Corey wrote: $text =~ tr{\t}{ }; $text =~ tr{\n}{ }; $text =~ tr{\r}{ }; $text =~ tr{\f}{ }; $text =~ tr{ }{ }s; That can be written as: tr/\t\n\r\f/ /, tr/ / /s for $text; But it doesn't remove all leading nor all trailing spaces. -- Ruud -- To unsubscribe, e-mail: beginners-uns

Re: Regexp to remove spaces

2009-12-21 Thread Dr.Ruud
sftriman wrote: I use this series of regexp all over the place to clean up lines of text: $x=~s/^\s+//g; $x=~s/\s+$//g; $x=~s/\s+/ /g; in that order, and note the final one replace \s+ with a single space. The g-modifier on the first 2 is bogus (unless you would add an m-modifier). I current

Re: Regexp to remove spaces

2009-12-20 Thread Robert Wohlfarth
On Sat, Dec 19, 2009 at 9:13 PM, sftriman wrote: > I use this series of regexp all over the place to clean up lines of > text: > > $x=~s/^\s+//g; > $x=~s/\s+$//g; > $x=~s/\s+/ /g; > > in that order, and note the final one replace \s+ with a single space. > > Basically, it's (1) remove all leading

Re: Regexp to remove spaces

2009-12-20 Thread Shawn H Corey
John W. Krahn wrote: > That can be reduced to: > > $text =~ tr/ \t\n\r\f/ /s; > > But that still doesn't remove leading and trailing whitespace so add two > more lines: > > $text =~ tr/ \t\n\r\f/ /s; > $text =~ s/\A //; > $text =~ s/ \z//; That was left as an exercise to the reader. Come now,

Re: Regexp to remove spaces

2009-12-20 Thread John W. Krahn
Shawn H Corey wrote: sftriman wrote: I use this series of regexp all over the place to clean up lines of text: $x=~s/^\s+//g; $x=~s/\s+$//g; $x=~s/\s+/ /g; in that order, and note the final one replace \s+ with a single space. Basically, it's (1) remove all leading space, (2) remove all trail

Re: Regexp to remove spaces

2009-12-20 Thread Shawn H Corey
sftriman wrote: > I use this series of regexp all over the place to clean up lines of > text: > > $x=~s/^\s+//g; > $x=~s/\s+$//g; > $x=~s/\s+/ /g; > > in that order, and note the final one replace \s+ with a single space. > > Basically, it's (1) remove all leading space, (2) remove all trailing

Re: Regexp to remove spaces

2009-12-20 Thread Erez Schatz
2009/12/20 sftriman : > I use this series of regexp all over the place to clean up lines of > text: > > $x=~s/^\s+//g; > $x=~s/\s+$//g; > $x=~s/\s+/ /g; > You can probably use $x=~s/^(\s+)|(\s+)$//g; But I don't think it will use any less CPU than the 3 regex option, the nature of Perl's regex en