Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Make things a *little* easier

by charnos (Friar)
on Sep 11, 2002 at 20:37 UTC ( #197069=note: print w/replies, xml ) Need Help??


in reply to a farewell to chop

Mostly, I agree with your point of view..chop isn't always needed, but killing off a function after years of use (especially one that presumably takes up so little space in the scheme of things) and is still somewhat relevant with no exact replacement, makes little sense to me. However, as per this statement: "...but I would have to look up the arguments in the docs, to tell it to locate from index -1 through the end, and replace with nothing", I believe that this can be accomplished a little simpler (at least to me) as thus:

$str='abcde'; $str = substr $str,0,(length($str)-1); print $str;
Which effectively truncates the last character, as chop() does. To me, that makes almost as much sense as chop does though, so beyond golf or obfuscation, I personally won't miss it much (I don't have that many legacy scripts that need to hack off one char).A chop() method in the string class with variable length would be nice as well :)
-Marc

Replies are listed 'Best First'.
Re: Make things a *little* easier
by mdillon (Priest) on Sep 11, 2002 at 20:52 UTC
    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.

      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.

Re: Make things a *little* easier
by John M. Dlugosz (Monsignor) on Sep 11, 2002 at 20:46 UTC
    Copy all except the last vs remove the last; the latter could be much more efficient for a large string.
    substr ($str, -1, 1, '');
    is what I was thinking.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2019-07-19 20:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?