Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

RE: RE: clearer code

by Simplicus (Monk)
on Apr 19, 2000 at 16:45 UTC ( [id://8027]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2025-07-15 12:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.