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
In reply to Re: Deleting intermediate whitespaces, but leaving one behind each word
by Laurent_R
in thread Deleting intermediate whitespaces, but leaving one behind each word
by Feneden
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |