Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: simplifying substitution

by Polyglot (Chaplain)
on Mar 08, 2021 at 13:00 UTC ( [id://11129326]=note: print w/replies, xml ) Need Help??


in reply to Re: simplifying substitution
in thread simplifying substitution

Maybe my perl version is different, but I can never get away with using actual spaces like that on the match side--I'd have to represent them...something like this:

$all =~ s/([ ]{2}[1-9][0-9])[ ]/$1\t\t/g;

If running strictly on English and there were no possibilities or no issue with encountering numbers like '04' which had to be handled differently, the code could be a mite simpler:

$all =~ s/([ ]{2}\d\d)[ ]/$1\t\t/g;

Blessings,

~Polyglot~

Replies are listed 'Best First'.
Re^3: simplifying substitution
by choroba (Cardinal) on Mar 08, 2021 at 13:06 UTC
    Without the /x modifier, a space should match literally. What's the output of
    perl -lwe 'print qq( $]) =~ s/ /v/r'
    (use double quotes instead on MSWin)

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      I get this output from that command (I'm not on Windows):

      Bareword found where operator expected at -e line 1, near "s/ /v/r" Unquoted string "r" may clash with future reserved word at -e line 1. Can't modify string in substitution (s///) at -e line 1, near "s/ /v/r " syntax error at -e line 1, near "s/ /v/r " Execution of -e aborted due to compilation errors.

      When I check the version, I get this:

      perl -v This is perl 5, version 12, subversion 4 (v5.12.4) built for darwin-th +read-multi-2level (with 2 registered patches, see perl -V for more detail) Copyright 1987-2010, Larry Wall

      Blessings,

      ~Polyglot~

        OK, 5.12 doesn't have /r. So what about
        perl -lwe '$v = qq( $]); $v =~ s/ /v/; print $v'
        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re^3: simplifying substitution
by Konabob (Initiate) on Mar 08, 2021 at 15:36 UTC

    Thanks to all of you, for your contributions! Since my script will be dealing strictly with English text, I decided to try "$all =~ s/( {2}\d\d) /$1\t\t/g;" first, and it seems to do the trick!
    Aloha, and stay safe!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11129326]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-25 23:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found