Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Re: Re: Efficienty truncating a long string

by PodMaster (Abbot)
on Dec 18, 2003 at 09:39 UTC ( [id://315473]=note: print w/replies, xml ) Need Help??


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

I don't know whether substr is optimized not to bother returning anything in void context (probably is), but this is how you do it
my $long_string = 'string' x 5; print $long_string,$/; substr( $long_string, 6 ) = ''; # truncate to 6 characters # # same thing, only using 4 arg substr # substr( $long_string, 6, length($long_string) - 6, '' ); # print $long_string,$/; __END__ stringstringstringstringstring string

MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
** The third rule of perl club is a statement of fact: pod is sexy.

Replies are listed 'Best First'.
Re: Re: Re: Re: Efficienty truncating a long string
by ysth (Canon) on Dec 18, 2003 at 10:03 UTC
    4-arg substr isn't optimized for void context. It will set up the replaced string to be returned regardless.

    The lvalue substr doesn't actually return the string but a special lvalue indicating the offset and length. Creating this and setting it to '' involves a fair amount of overhead.

    Which is actually more efficient will probably depend on how much you are truncating.

      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.
        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.
Re: Re: Re: Re: Efficienty truncating a long string
by dino (Sexton) on Dec 18, 2003 at 09:50 UTC
    Ty, How embarrasing, that simple eh? Thanks again, sigh. :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-04-23 19:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found