Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

by Athanasius (Archbishop)
on Dec 05, 2017 at 06:35 UTC ( [id://1204928]=note: print w/replies, xml ) Need Help??


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

Hello Feneden, and welcome to the Monastery!

No need for the join, just replace each occurrence of one or more whitespace characters with a single character:

16:30 >perl -wE "my $s = 'Intel(R) Xeon(R) CPU X5660 2.80GHz + '; $s =~ s/(\s)+/$1/g; say qq[>$s<];" >Intel(R) Xeon(R) CPU X5660 2.80GHz < 16:33 >

Update: Looking again at the thread title, it appears you may also want to remove trailing whitespace at the end of the string:

use strict; use warnings; my $s = 'Intel(R) Xeon(R) CPU X5660 2.80GHz '; $s =~ s{ (\s)+ }{$1}gx; $s =~ s{ \s+ $ }{}x; print "\n>$s<\n";

Output:

17:26 >perl 1843_SoPW.pl >Intel(R) Xeon(R) CPU X5660 2.80GHz< 17:26 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: Deleting intermediate whitespaces, but leaving one behind each word
by Feneden (Acolyte) on Dec 05, 2017 at 07:50 UTC

    Hello Athanasius, iŽll try this later in my script. For now it seems like a great way. Also for me, as a Perl beginner, i was not too far away from the solution :) This is my first month in scripting, before i only had 2 month of experience in Java. All in all great "costumer" feeling here - Great experience using this forum. I appreciate your help :) See you soon :D

      Perlmonks is the best. So many helpful people. Welcome!

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest";
      $nysus = $PM . ' ' . $MCF;
      Click here if you love Perl Monks

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1204928]
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-18 04:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found