Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Make things a *little* easier

by mdillon (Priest)
on Sep 11, 2002 at 20:52 UTC ( #197073=note: print w/ replies, xml ) Need Help??


in reply to Make things a *little* easier
in thread a farewell to chop

Or, how about this, using lvalue substr:

$str = 'abcde'; substr($str, -1) = ''; print $str, $/;
Update: just in case anyone was wondering (as Solo did in a /msg), this is real Perl 5 code. I believe lvalue substr was added in some patchlevel release of Perl 5.005.

Update^2: the advantage this has over the 4-arg substr is that you don't need to put the number of chars to replace in the string (which would always be the absolute value of the negative offset in situations like this); it just replaces everything up through the end of the string.


Comment on Re: Make things a *little* easier
Download Code
Re: Re: Make things a *little* easier
by John M. Dlugosz (Monsignor) on Sep 12, 2002 at 14:49 UTC
    the advantage this has over the 4-arg substr is that you don't need to put the number of chars to replace in the string (which would always be the absolute value of the negative offset in situations like this);

    I noticed that. If you leave off the argument, it automatically takes the rest of the chars to the end. If you want to give a 4th argument, you can't leave off the 3rd.

    But, shouldn't passing undef for the 3rd argument mean the same thing as leaving it off? That's what I would expect.

      Yes. Using undef as the third argument does have that effect. I suspect Perl 6 will improve on this somewhat by being able to do: substr $str, -1, replace => '';
      This may not seem like much of an improvement, but it gets rid of the undef, which could possibly be mysterious to the uninitiated (as could lvalue substr).

      Update: yikes! Using undef doesn't actually work like this (i.e. as the third arg in a Perl-5 substr). I had assumed it did, but it actually acts the same as using 0 as the third arg (at least with 5.8.0). Anyways, the lvalue behavior is stable and documented since 5.6 (possibly earlier); if undef works like this in any Perl version, I haven't seen it documented. Sorry for the foolish mistake (and for the unfounded assumption).

      Now that would be something I'd like to see in Perl 6 (and which I have missed in the Perl 5 version of the function on a number of occasions).

      Makeshifts last the longest.

        You seem to be getting different results than mdillon. That's the problem with relying on undocumented details. If you write a sub starting with my ($a,$b,$c,$d)=@_; and then calculate defaults if undef or use the undef value for some special meaning, then leaving off args naturally gives the same results. But if you write if (scalar(@_) < 4) ... to do something different, then using undef doesn't give the same effect.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://197073]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2014-09-16 06:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (156 votes), past polls