On Sat, 27 Jan 2001 15:42:43 -0700, root wrote:
>I read RFC195 suggesting to drop 'chop' and go with 'chomp'.
>What does 'chop' have anything to do with 'chomp'?
>I'm totally oppose to that. Consider:
>
>my $s;
>map { /\S/ && $s .= "$_ " } split(/\s+/,@_);
>chop($s);
>return $s;
Excuse me, but you're using chop() for a task it wasn't invented for.
Think about joining your strings with more than one character.
my $s;
map { /\S/ && $s .= "$_ - " } split(/\s+/,@_);
chop($s);
return $s;
That doesn't quite cut it, does it?
Here's how you should have written your code:
return join ' ', grep { /\S/ } split(/\s+/,@_);
I, too, once used chop() to get the last character of a string, in my
case to calculate a barcode check digit.
while(my $digit = chop($barcode)) {
...
}
The while loop should have continued until $barcode was empty, all
digitis consumed. Well, the fact that "0" is false really spoilt it for
me.
And in case you're wondering why I wanted to process a barcode from the
back: because the total number of dogots isn't always the same, and the
last digit is the only one you're always sure of on how it ought to be
processed. For the following digits, you always have to toggle the
behaviour of the processing.
The full story is: chop() is not a generic operator, but one
specifically intended for one dedicated task: dropping the newline from
a string read from a file. If you use it for anything else, it probably
sooner or later will bite you. And it's not particularily good at what
it *was* designed for, e.g. with a file not ending in a newline.
--
Bart.