To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.off-topic.geekOpen lugnet.off-topic.geek in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 Off-Topic / Geek / 2246
2245  |  2247
Subject: 
Re: Strip surplus white space with Perl
Newsgroups: 
lugnet.off-topic.geek
Date: 
Sun, 29 Oct 2000 21:25:03 GMT
Viewed: 
122 times
  
In lugnet.off-topic.geek, Christopher Lindsey writes:
Christopher Lindsey wrote:
Ahh, but what happens if you have a carriage return followed by a
space?  The second method eliminates the space and leaves the
carriage return, which isn't what he asked for.

Ahh, but that *is* what he asked for.  Fredrik said, "If two consecutive
characters are <SPACE> and <RET>, for example, it doesn't matter to me
which one is preserved and which one is chopped off."


[...]
This should do it:

   $string =~ s/(\t\t+|  +|\r\r+|\n\n+|\f\f+)/substr($&,1,1)/eg;

My goodness.  I suppose you could do it that way, but why go through all that
trouble when all you want to do is this?--

   $string =~ s/(\s)\1+/$1/g;

Or, if you want to enumerate whitespace characters explicitly (because \n
isn't guaranteed to map to \012 and \r isn't guaranteed to map to \015):

   $string =~ s/([\009\040\015\012])\1+/$1/g;


Yes, a /g will work here, David was right about that.  I usually stick
with a while loop because in some cases it's better (i.e. if you're
using $` and don't want to recalculate things like length from the start
of the string each time).

Could you give a concrete example?  I'm curious -- I've always believed that
/g is faster (and clearer code) than using a while, even when there aren't
lots of matches.  Doesn't

   while ($string =~ s///) {}

have O(n^2) performance, where n is the number of hits in the string?  Or are
you hiding some anchors in there?

--Todd



Message has 2 Replies:
  Re: Strip surplus white space with Perl
 
(...) s/009/011/ oops :) --Todd (24 years ago, 29-Oct-00, to lugnet.off-topic.geek)
  Re: Strip surplus white space with Perl
 
(...) There are some days when I shouldn't touch a keyboard. I read and reread his initial posting and could have sworn that it said "It *does* matter to me which one..." I think you just went in and edited the message. ;) I guess it's one of those (...) (24 years ago, 29-Oct-00, to lugnet.off-topic.geek)

Message is in Reply To:
  Re: Strip surplus white space with Perl
 
(...) I hate replying to myself, but I realize that I pasted the wrong answer into my initial response now, and totally missed the boat on my response to David. Ooops. :) This should do it: $string =~ s/(\t\t+| +|\r\r+|\n\n+|\f\f+)...&,1,1)/eg; Yes, (...) (24 years ago, 29-Oct-00, to lugnet.off-topic.geek)

10 Messages in This Thread:



Entire Thread on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact

This Message and its Replies on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    

Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR