in reply to Re: Substitution in range operator
in thread Substitution in range operator

Thanks for your response,

I created an output file using my original script because that is what puts the pipes in

## Substitute 2 or more horizontal white space with pipe s/[\h+]{2,}/|/g;

then i passed that file through your script it finds the pipe and replaces it with a space but it is still on a separate line.

#!/usr/bin/perl local $/; open my $fh, '<', 'whitespacegone.txt' or die $!; my $txt = <$fh>; $txt =~ s/(\n\|)/ /mg; for ( split '\n', $txt ) { # process each line } print $txt;

Replies are listed 'Best First'.
Re^3: Substitution in range operator
by 1nickt (Canon) on Dec 30, 2015 at 05:18 UTC

    Hm, odd. Please try running this program:

    #!/usr/bin/perl use strict; use warnings; local $/; my $txt = <DATA>; $txt =~ s/\n\|/ /g; print $txt; __DATA__ foo | bar baz |qux fred |wilma barney
    Output should be :
    foo | bar baz qux fred wilma barney
    (Note that you don't need to enclose your desired match in parentheses unless you need to "capture" it.)


    The way forward always starts with a minimal test.

      Output:

      foo | bar baz qux fred wilma barney

      Perl -v perl 5, version 14 subversion 2 (v5.14.2) built for cygwin-thread-multi-64int

        I'm not sure if it is an issue with the Perl version (I am using 5.16.3 and 5.22), but rather with line endings on Windows. I don't use Windows myself, but there are considerations with line-endings on that platform; do a search and you'll find at least a couple of discussions about the topic in the last couple of months.

        What happens if you change the key line to:

        $txt =~ s/\r\n\|/ /g;
        ?


        The way forward always starts with a minimal test.