Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: Efficienty truncating a long string

by dino (Sexton)
on Dec 18, 2003 at 10:28 UTC ( #315485=note: print w/replies, xml ) Need Help??

in reply to Re: Re: Re: Re: Efficienty truncating a long string
in thread Efficienty truncating a long string

It was originally part of a log reader. I found that reading in blocks and working pattern matching on those blocks was much more efficient than on line by line. Obviously you can get partial lines, so I rindex the last /n, save the fragment at the end for the next pass and return the main block truncated. It works like a charm. I was reinventing it and seing how I could make it more efficient.
  • Comment on Re: Efficienty truncating a long string

Replies are listed 'Best First'.
Re: Re: Efficienty truncating a long string
by ysth (Canon) on Dec 18, 2003 at 16:40 UTC
    Then you probably want to use 4-arg substr to truncate the buffer and save the fragment all at once:
    $end = rindex($buffer, "\n"); if ($end < 0) { whoops, incomplete line } $fragment = substr($buffer, $end+1, length($buffer), '');
    Note that length($buffer) is bigger than the actual length(buffer)-$end-1 you want, but specifying a length larger than is available is guaranteed to not cause problems; you'll just get what is there. If the buffer ends with "\n", you'll get an empty $fragment.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (1)
As of 2023-05-31 04:33 GMT
Find Nodes?
    Voting Booth?

    No recent polls found