in reply to RE: RE: clearer code
in thread clearer code

Anchoring a regex (with ^ or $) will nearly always make it more efficient, as it tells the engine that the expression can only match starting from (or ending with) one position.

In this case, though, when you know you want to get rid of 129 characters, substr will definitely be faster. Something like: substr $text, 0, 129, ''; (oops, it *should* be length and not offset. Thanks btrott!)

Replies are listed 'Best First'.
RE: RE: RE: RE: clearer code
by btrott (Parson) on Apr 19, 2000 at 20:34 UTC
    I was going to say that that looked wrong, but then I looked at the docs online (my work has 5.004_04) and found that that was valid. That's pretty nice. :)

    Although I think it should be 129 instead of 128, because it's not the index into the string, it's the length that you want to cut out... which is 129, right?

    So it should actually be

    substr $text, 0, 129, '';
    Other possibilities using substr:
    substr($text, 0, 129) = '';
    or
    $text = substr $text, 129;
    The first uses substr as an lvalue, setting the first 129 characters to ''. Or eliminating them, in other words.

    The second is more "standard"; it sets $text to characters 129-the end of $text.

    All have the same result.