RegExp Search/Replace is a better match for this kind of job. But I also would like to show how to do this via split/join. This might come in handy if you want to skip certain fields or need an array representation later on.

The split pattern

I see you're using the pattern 'a single space' / /, but since you want to fold whitespace anyway a better solution is to use 'multiple spaces' / +/ or 'any whitespace' /\s+/, the effect is this:

my $CPU = 'Intel(R) Xeon(R) CPU X5660 2.80GHz '; # field: 0 1 2 3..11 12 13 14 # using / / # field: 0 1 2 3 4 # using / +/

Usage of join

There are multiple ways to feed the generated array into join. In order to get the expected result you can either feed each field separately, use an array slice or just feed the whole array:

my @CPU_SPLIT = split / +/, 'Intel(R) Xeon(R) CPU X5660 2.80 +GHz '; # 1 - feed each field explicitly print join ' ', $CPU_SPLIT[0], $CPU_SPLIT[1], $CPU_SPLIT[2], $CPU_SPLI +T[3], $CPU_SPLIT[4]; print "\n"; # 2a - use an array slice - explicitly print join ' ', @CPU_SPLIT[0,1,2,3,4]; print ">n"; # 2b - use an array slice - via a range print join ' ', @CPU_SPLIT[0..4]; print "\n"; # 3 - feed the whole array print join ' ', @CPU_SPLIT; print "\n";

In reply to Re: Deleting intermediate whitespaces, but leaving one behind each word by Monk::Thomas
in thread Deleting intermediate whitespaces, but leaving one behind each word by Feneden

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.