I didn't put a lot of brainpower into this; just wanted to share a thought of improvement. Enjoy!
sub mchomp { my @a=@_; local $_; foreach (@a) { if (ref $_ eq 'SCALAR') { $$_=~s/\s+$//; } elsif (ref $_ eq 'ARRAY') { @$_=mchomp(@$_); } elsif (ref $_ eq 'HASH') { for my $k (keys %$_) { $_->{$k}=mchomp($_->{$k}); } } else { s/\s+$//; } } wantarray ? @a: $a[0]; }
Examples
## returns the modified string print mchomp $string; ## modifies the variable directly mchomp \$string; print $string; ## returns an array of modified strings print join ",",mchomp @array; ## modifes the array inplace mchomp \@array; print join ",",@array; ## returns the modified hash %new=mchomp %hash;print values %new; ## modifes the hash inplace mchomp \%hash;print values %hash;
In reply to Re: Chomping Away (was Ovid Golf Chomps)
by mr.nick
in thread How do you chomp your chomps?
by kaatunut
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |