in reply to Fastest (Golfish) way to strip whitspace off ends.

I realise this has GOLF in the title, and I'm not the squeamish type, but seeing functions written to rely upon them being given their argument in $_ makes my skin crawl :)

For one thing, there is no need or point in actually passing the argument to the functions as A( $_ ), as all you are doing is stacking a variable which you then don't use inside the function.

Second, it your going to write functions that emulate some of the built-ins and use $_ by default, you should at least check to see if you have been passed any arguments and operate on them if you have, and only apply the default if you haven't.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


  • Comment on Re: Fastest (Golfish) way to strip whitspace off ends.

Replies are listed 'Best First'.
Re: Re: Fastest (Golfish) way to strip whitspace off ends.
by halley (Prior) on Jul 02, 2003 at 17:02 UTC
    How should that be implemented? I think this should work just like all forms of the built-in chomp except it should remove more than trailing newlines. However, the argumentless form doesn't seem to work (doesn't propagate the lvalue context on the caller's $_) as I would expect it should. What's the right way of falling back to the caller's lvalue $_ if there's no arguments?
    sub trim { @_ = ($_) if not @_; # doesn't work foreach (@_) { s/^\s*//; s/\s*$//; s/\n\z//s; } return wantarray? @_ : $_[0]; }

    --
    [ e d @ h a l l e y . c c ]

      Here's a couple of ways, but neither is necessarially the best way.

      sub trim { @_ = ($_) if not @_; foreach my $s (@_) { $s =~ s/^\s*//; $s =~ s/\s*$//; $s =~ s/\n\z//s; } return wantarray? @_ : $_[0]; }

      Or

      sub trim { @_ = ($_) if not @_; local $_; foreach (@_) { s/^\s*//; s/\s*$//; s/\n\z//s; } return wantarray? @_ : $_[0]; }

      I think that the problem with your implementation was that you were stomping on the aliasing of $_ by re-using the global $_ implicitly in your foreach loop. Using either a my'd var or localising $_ after the assignment to @_ but before the foreach loop seems to fix the problem, though I have a gut feel that there is probably a better way.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


      Halley,

      I am curious as to what s/\n\z//s would actually substitute that the previous two substitutions already had not.

      AFAICT, the \n\z would match any newlines before the end of the string, which should have been already taken care of s/\s*$//; (as \n is matched by \s) should have captured anyway (i might be missing something here, so please correct me if i am wrong.)

      Also, I believe that \s+ should be faster(more efficient) in this case than \s* .

      -enlil

Re: Re: Fastest (Golfish) way to strip whitspace off ends.
by EvdB (Deacon) on Jul 03, 2003 at 09:07 UTC
    You are absolutely right that the functions as presented are lacking in several ways. Also the useless passing of $_ is duly noted.

    In my defence I would say that this was just an exercise in looking at the relative speed of the methods, and so much of the sanity checking and so on was omitted for clarity. The functions are just regexps - no more no less. That way all of the speed differential is due to the stripping of whitespace and very else.

    --tidiness is the memory loss of environmental mnemonics