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

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

Expand Messages
  • diheptal <diheptal@talk21.com>
    can anyone change code below a bit? what this does to a plain text file is overwrite all the characters with backslashes. it does not at present overwrite any
    Message 1 of 3 , Feb 3 6:33 AM
    • 0 Attachment
      can anyone change code below a bit?

      what this does to a plain text file is overwrite all the
      characters with backslashes. it does not at present overwrite
      any whitespace.

      what i am trying to do is -

      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.

      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]


      This is my code: i'm working in microsoft until i sort out my
      free software situation
      [really, it's going to happen...]:

      wayne
      ############

      #!/usr/bin/perl -w

      open(F1, "C:\\Windows\\Desktop\\Wayne\\sentence.txt") || die;

      open(F2, ">C:\\Windows\\Desktop\\Wayne\\sentencem3.txt") || die;


      while(<F1>)
      {
      ($line = $_) =~ s/[^\ \s+]/\\/g;

      print F2 $line;
      }

      close(F1);
      close(F2);
    • Charles K. Clarkson
      ... In combination, these two rules specify that each line must lose any trailing spaces and have a backslash added: s/ s+$//; $_ .= ; Or: s/ s*$/ /; ...
      Message 2 of 3 , Feb 3 11:35 PM
      • 0 Attachment
        diheptal <diheptal@...> wrote:

        : what i am trying to do is -
        :
        : 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
        added:

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

        Or:

        s/\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.

        [snip]

        : while(<F1>)
        : {
        : ($line = $_) =~ s/[^\ \s+]/\\/g;
        :
        : print F2 $line;
        : }

        while ( <F1> ) {
        s/\s*$/\\/;
        s/\b(\s+)/'\\' x length $1/eg;
        print OUT;
        }

        : close(F1);
        : close(F2);


        HTH,

        Charles K. Clarkson
        --
        Head Bottle Washer,
        Clarkson Energy Homes, Inc.
        Mobile Home Specialists
        254 968-8328
      • 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 3 of 3 , Feb 4 9:04 AM
        • 0 Attachment
          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
          >added:

          > 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

          s/(?:\G|\b)\s/\\/g

          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.