in reply to RE: clearer code
in thread clearer code

the s/.{129}// was to remove the first 129 characters of the file. This was header info only, and confused the issue. Maybe I should have used ^, but since there is no g at the end of the line, doesn't it amount to the same thing? I mean the regex is going to be searched from the top each time, isn't it?
Seems to work, but am I being sloppy?
Simplicus.

Replies are listed 'Best First'.
RE: RE: RE: clearer code
by chromatic (Archbishop) on Apr 19, 2000 at 20:02 UTC
    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!)

      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.