Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: remove spaces after join operation

by AnomalousMonk (Monsignor)
on Jul 26, 2013 at 14:41 UTC ( #1046525=note: print w/ replies, xml ) Need Help??


in reply to remove spaces after join operation

Others have noted the approach of join-ing a list of strings with a space and then taking spaces out of the string where appropriate. Conceptually, the other approach is to not put in a space in the first place. See List::Util::reduce().

>perl -wMstrict -le "my @first_sentence = qw{ i am 2 ( but ) . }; printf qq{'$_' } for @first_sentence; print ''; ;; my $ns = qr{ [().] }xms; ;; my $sentence = join q{ }, @first_sentence; print qq{'$sentence'}; ;; $sentence =~ s{ [ ] (?= $ns) | (?<= $ns) [ ] }''xmsg; print qq{'$sentence'}; ;; ;; use List::Util qw(reduce); ;; $sentence = ''; $sentence = reduce { ($a =~ $ns || $b =~ $ns) ? qq{$a$b} : qq{$a $b} } @first_sentence ; print qq{'$sentence'} " 'i' 'am' '2' '(' 'but' ')' '.' 'i am 2 ( but ) .' 'i am 2(but).' 'i am 2(but).'

Update: Actually, this is what 2teez already did here.


Comment on Re: remove spaces after join operation
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2014-08-02 02:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (53 votes), past polls