Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: fast string parser: regex versus substr

by mce (Curate)
on Nov 20, 2003 at 16:55 UTC ( #308622=note: print w/replies, xml ) Need Help??


in reply to fast string parser: regex versus substr

Thanks all,

I came up with.

sub dopack { foreach my $i ( 0..$#data ) { my $line=$data[$i]; my ($jcpu,$j,$s)=(unpack('@2A16A40A232A16', $line))[0,1,3]; warn "$jcpu $j $s"; # ..store the values in a hash, but that is not important here } }
And it won the benchmark competition :-)

I never really understood pack and unpack, but it am getting to like it.
---------------------------
Dr. Mark Ceulemans
Senior Consultant
BMC, Belgium

Replies are listed 'Best First'.
Re: Re: fast string parser: regex versus substr
by bart (Canon) on Nov 20, 2003 at 22:24 UTC
    my ($jcpu,$j,$s)=(unpack('@2A16A40A232A16', $line))[0,1,3];
    I think you'd be better off using the "x" template to ignore the third field (with index 2):
    my ($jcpu,$j,$s)= unpack('@2A16A40x232A16', $line);

    You can replace the leading '@2' with 'x2'', too. Or, just the reverse, replace the 'x232' with '@290'. I don't think it'll make much difference speedwise.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://308622]
help
Chatterbox?
[davido]: And remember; time is hard. :) As soon as you think, I've got this, you'll discover that time is harder than that. :)
[1nickt]: TCLion are you parsing the dates with DateTime or another tool?
[1nickt]: I note DateTime::Format:: Flexible, announced immediately below this box in the CPAN feed
[Corion]: 1nickt: That one sounds sensible as it will tell you when it found an error
[1nickt]: Throws on error, I believe
[1nickt]: But should handle month as number or word

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (14)
As of 2017-03-23 14:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (287 votes). Check out past polls.