Loading ...
Sorry, an error occurred while loading the content.

RE: [PBML] substituting white space with a character [i.e .a backslash].

Expand Messages
  • Jeff 'japhy' Pinyan
    ... You need instead of just . ... Sad to say, that s AWFULLY inefficient. Here s a place where the sexeger approach really shines: use Benchmark
    Message 1 of 3 , Feb 4, 2003
      On Feb 4, Charles K. Clarkson said:

      >: 1. if there is one whitespace [or more] at the end of a line:
      >: replace with one [and one only] backslash.
      >: 1.a. if no whitespace at the end:
      >: add one backslash.
      > In combination, these two rules specify that each
      >line must lose any trailing spaces and have a backslash

      > s/\s+$//;
      > $_ .= '\';

      You need '\\' instead of just '\'.

      > s/\s*$/\\/;

      Sad to say, that's AWFULLY inefficient. Here's a place where the sexeger
      approach really shines:

      use Benchmark 'cmpthese';
      use strict;

      my $str = "this is a big pile of alphabet soup right here! ";

      cmpthese(-5, {
      charles => sub { (my $s = $str) =~ s/\s*$/\\/ },
      japhy => sub { (my $s = reverse $str) =~ s/^\s+/\\/; $s = reverse $s },

      >: 2. if there is any leading whitespace [i.e. start of line]:
      >: leave it intact [i.e. do not substitute with backslashes].
      >: 3. any internal white space in a line, i.e. between words:
      >: substitute with the same number of backslashes:
      >: [i.e. a one for one substitution]
      > s/\b(\s+)/'\\' x length $1/eg;
      > I think there's a way to do it better with \G, but
      >I never seem to get it going.

      Well, \G doesn't seem to lend itself to any efficiency in this case,
      really. If you were thinking of


      then it's not faster than the s///eg method. In fact, it runs at 2/3 the
      speed of the s///eg method. And it's really good that \b doesn't match a
      leading space, since \G would.

      I'd do:

      while (<IN>) {
      s/\b(\s+)/'\\' x length $1/eg;
      ($_ = reverse) =~ s/^\s+/\\/;
      $_ = reverse;
      print OUT;

      Jeff "japhy" Pinyan japhy@... http://www.pobox.com/~japhy/
      RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/
      <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
      [ I'm looking for programming work. If you like my work, let me know. ]
    Your message has been successfully submitted and would be delivered to recipients shortly.