Note that split / +/, $string doesn't handle leading whitespace gracefully:
Here, the special-case ' ' split pattern is better (if you're going to split/join):c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my $CPU = ' Intel(R) Xeon(R) CPU X5660 2.80GHz '; dd $CPU; ;; my @CPU_SPLIT = split / +/, $CPU; dd \@CPU_SPLIT; ;; my $t = join ' ', @CPU_SPLIT; dd $t; " " Intel(R) Xeon(R) CPU X5660 2.80GHz " ["", "Intel(R)", "Xeon(R)", "CPU", "X5660", "2.80GHz"] " Intel(R) Xeon(R) CPU X5660 2.80GHz"
(Dealing with general whitespace was exactly why ' ' was special-cased.)c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my $CPU = ' Intel(R) Xeon(R) CPU X5660 2.80GHz '; dd $CPU; ;; my @CPU_SPLIT = split ' ', $CPU; dd \@CPU_SPLIT; ;; my $t = join ' ', @CPU_SPLIT; dd $t; " " Intel(R) Xeon(R) CPU X5660 2.80GHz " ["Intel(R)", "Xeon(R)", "CPU", "X5660", "2.80GHz"] "Intel(R) Xeon(R) CPU X5660 2.80GHz"
Give a man a fish: <%-{-{-{-<
In reply to Re^2: Deleting intermediate whitespaces, but leaving one behind each word
by AnomalousMonk
in thread Deleting intermediate whitespaces, but leaving one behind each word
by Feneden
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |