Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: (tye)Re: How do you chomp your chomps?

by Anonymous Monk
on Jan 09, 2001 at 04:11 UTC ( #50598=note: print w/replies, xml ) Need Help??

in reply to (tye)Re: How do you chomp your chomps?
in thread How do you chomp your chomps?

And, of course, one could always write their own little function such as:
sub my_chomp { my $str = shift; chomp($str); return $str; }
Though this doesn't handle arrays, it also doesn't modify the input string in place.

Replies are listed 'Best First'.
(Ovid -Golf) Re(3): How do you chomp your chomps?
by Ovid (Cardinal) on Jan 09, 2001 at 04:55 UTC
    Well, heck. If we're gonna go that route, may as well golfuscate it:
    sub my_chomp { substr shift, 0, (length) - 1 if $_[0] =~ /\n$/}; # :-)


    Update: Can anyone tell that I am *seriously* bored at work?

    sub my_chomp { wantarray ? (map{ /\n$/ ? substr $_, 0, (length)-1 : $_ + } @_) : chomp(my $x = shift),$x };
    That ugly hack will work for both arrays and scalars (I think). I noticed that it appears to be returning an empty element at the end of the array, though. :(

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      All fine and good while $/ is \n. Dunno about you :), but I've been known to change it now and then:

      sub my_chomp { substr shift, 0, ((length) = length($/)) if $_[0] =~ /$\$/};

      Untested, but I would like to point out that the regex is pretty.

      Why not go ahead and Make A Better Chomp(tm)? Here's my first stab at it; it basically follows the rules of:
      1. Currently just strips all trailing white space
      2. If passed a reference, operate on it directly (duh! But also works for passing arrays of references) and return a dud value
      3. Otherwise return the modified string(s) without touching the originals.
      4. Works on scalar, arrays and hashes (why not? :) It modifes the values, not the keys, though it would be no problem to add that, too)
      5. Recursive (didn't a just read something here about "real" uses of recursion?) so you can use wacky data structures.

      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]; }


      ## 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;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://50598]
erix just did a 12 km-run. Slow, but unstoppable!
[choroba]: My recent record is 10km in 1 hour :)
[perldigious]: Nice erix... perldigious' legs are so sore from portaging his kayak from place to place last weekend he can barely hobble 12m. :-)
[erix]: you're faster than me then :)
[erix]: well, at the moment, that is :)
[erix]: I should lose some weight although I keep telling myself that I'm doing extra training by lugging it along
[erix]: I saw a common kingfisher and a couple of bullfinches
[erix]: bullfinch ( a male and a female )

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2017-05-24 12:44 GMT
Find Nodes?
    Voting Booth?