Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: contextual substitution with s///?

by BrowserUk (Pope)
on Feb 01, 2013 at 16:27 UTC ( #1016578=note: print w/ replies, xml ) Need Help??


in reply to contextual substitution with s///?

I want to remove one of those spaces (so I can 'split()' the line on spaces consistantly)

Seems like your working hard to do something that can be far more easily achieved.

The problem you are trying to fix is when you split on \s. (Here I've substituted _ for spaces to make things clearer.):

$s = 'The_quick__brown____fox';; print for split /_/, $s;; The quick brown fox

Instead of trying to remove the extra spaces, simply accommodate them:

print for split /_+/, $s;; The quick brown fox

Problem solved.

In addition, as splitting on variable amounts of whitespace is such a common thing to do, if you supply split with a single space (' ') in place of the regex argument, it takes care of that (and any leading whitespace) for you:

print for split / /, ' the quick brown fox ';; the quick brown fox print for split ' ', ' the quick brown fox ';; the quick brown fox

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.


Comment on Re: contextual substitution with s///?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (12)
As of 2014-12-19 10:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (77 votes), past polls