in reply to Remove whitespace in string and maintain overall length
Note that I've also made the code agnostic as to the kind of whitespace.my $address = "$strnum $strname $strtype $strdir"; $address =~ s/\s+/ /g; $address .= ' ' while length $address < 37;
That approach has a lot more operations, but reads easily. Another question to consider is are address guaranteed to be less than 38 characters, and if not should you truncate. So you might do that one as
where I've truncated to 37, used sprintf and the r modifier, and put it on one line to make it as inobvious as possible.my $address = sprintf '%-37.37s', "$strnum $strname $strtype $strdir" +=~ s/\s+/ /rg;
#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Remove whitespace in string and maintain overall length
by silentbob343 (Initiate) on Nov 19, 2015 at 19:25 UTC |