Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Deleting intermediate whitespaces, but leaving one behind each word

by Laurent_R (Canon)
on Dec 05, 2017 at 09:07 UTC ( [id://1204938]=note: print w/replies, xml ) Need Help??


in reply to Deleting intermediate whitespaces, but leaving one behind each word

Hi Feneden,

the solutions using a simple regex suggested by Athanasius and haukex are great and definitely better than any solution using split and join (and I would have suggested exactly the same solution as haukex if he had not done it before).

However, I feel it might be useful for you to see how your original solution could be improved:

use strict; use warnings; my @CPU_SPLIT = split /\s+/, $CPU; # split the string on one or mo +re spaces # ~ s\ //g; # this line does not make any s +ense to me, commented out. my $result = join ' ', @CPU_SPLIT; # join can operate directly on +an array, no need to list the indices

Replies are listed 'Best First'.
Re^2: Deleting intermediate whitespaces, but leaving one behind each word
by Eily (Monsignor) on Dec 05, 2017 at 09:21 UTC

    definitely better than any solution using split and join
    Well, the best solutions are the ones you understand the most. I doubt there's a performance issue here anyway. So ++ for giving a solution based on the original one.

    NB: split /\s+/, $CPU can be written split " ", $CPU since the latter will be translated to the former, as explained in the doc. Then if you don't need the temp variable:
    my $result = join " ", split " ", $CPU;
    (Okay, maybe in that case using /\s+/ makes it look a little less silly :P)

      Well, the best solutions are the ones you understand the most. I doubt there's a performance issue here anyway.
      Yes, Eily++, I agree with both sentences. I meant "better" only in the sense that I find that the solutions using the s/// substitution operator are just simpler.

      And, yes, I would also avoid the intermediate temp variable by pipe-lining the join and the split as you've shown, but, here, I wanted to stay close to the OP's solution.

      As for the more awkish version of split using the ' ' string for splitting on multiple spaces, I know it exists and I agree it looks somewhat simpler, but I tend to prefer a regex such as /\s+/ because I find it states more explicitly what it is doing; as an example, I wouldn't know for sure (off the top of my head, without looking up in the documentation or testing, that is) whether it would also split on tabs or new line characters.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-24 10:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found