Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

chopping lots of characters

by Anonymous Monk
on Jun 24, 2004 at 11:35 UTC ( #369291=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:


Quick and easy question i'm sure, but how can I chop off the last 4 characters from a string using chop.

I can do this:

chop($string); chop($string); chop($string); chop($string);

Thats a little bit ugly though.

I realise I can probably use substr, but wondered if there was a cool way to do it with chop.


Replies are listed 'Best First'.
Re: chopping lots of characters
by hmerrill (Friar) on Jun 24, 2004 at 12:12 UTC
    Yeah you can do that by calling chop 4 times, but that's 4 function calls. Without actually checking performance on this I would think(?) that one call to substring would be faster, but...
    $string = substr($string, 0, -4);
    that will give you $string containing all but the last 4 characters of what it started with.


      A quicky test shows that 3-arg substr is faster than chop, but a 4-arg substr is even faster - and that's to be expected because it saves a copy. Differences aren't dramatic though.
      #!/usr/bin/perl use strict; use warnings; use Benchmark qw /cmpthese/; chomp (our @strings = <DATA>); cmpthese -5 => { chop => 'my @copy = @strings; foreach my $str (@copy) { chop $str; chop $str; chop $str; chop $str; }', substr3 => 'my @copy = @strings; foreach my $str (@copy) { $str = substr $str => 0, -4; }', substr4 => 'my @copy = @strings; foreach my $str (@copy) { substr $str => -4, 4 => ""; }', }; __DATA__ asdfjas;dfjas;dfjas;dkfjasdfasdf asd sdfajsd;flaks Rate chop substr3 substr4 chop 152166/s -- -11% -14% substr3 170187/s 12% -- -4% substr4 176800/s 16% 4% --
Re: chopping lots of characters
by Abigail-II (Bishop) on Jun 24, 2004 at 11:45 UTC
    As you said, substr is the way to go. chop chops off one character, and not more than one. So, if you want to chop off 4 characters using chop, you need to chop 4 times. Either by duplicating the line, or by using a loop. You might want to bail out if the string is empty:
    my $i = 4; chop $string while $i -- && length $string;
    my $i = 4; 1 while $i -- && defined chop $string;
    although the latter might want to chop an empty string (but only once).


      thanks thats cool, think I might just stick with chop repeated 4 times though - easier to read and about the same number of characters anyway ;-)
Re: chopping lots of characters
by dragonchild (Archbishop) on Jun 24, 2004 at 12:33 UTC
    I realise I can probably use substr, but wondered if there was a cool way to do it with chop.

    You mean, "I realize I probably should use substr, . . ." right? In other words, if I was your maintenance programmer, I would be very confused why you chose to repeat the same line four times instead of using substr. Memes develop for a reason.

    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

      Not sure what Memes are...? But yes you are right, I just couldn't be bothered to look up the syntax ;-) but now you've made me feel bad i'll change it!


Re: chopping lots of characters
by japhy (Canon) on Jun 24, 2004 at 12:50 UTC
    chop($x) for 1 .. 4;
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;
Re: chopping lots of characters
by Fletch (Chancellor) on Jun 24, 2004 at 11:57 UTC

    If the four characters are always the same you might consider setting $/ and using chomp instead.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://369291]
Approved by Happy-the-monk
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2018-01-22 18:59 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (235 votes). Check out past polls.