Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

(jjhorner)Regexp glitch while parsing record in CSV

by jjhorner (Hermit)
on Jul 17, 2000 at 06:35 UTC ( [id://22813]=note: print w/replies, xml ) Need Help??


in reply to Regexp glitch while parsing record in CSV

I'm not getting the same results as everyone else:

#!/usr/bin/perl -w use strict; use Benchmark; my $string = " field1 , field2 , field3 , field4 , field5 , field6 , field7 , field8 , field9 , field10 , field11 , field12 "; sub regexp { $string =~ m#^(\s*)(\w*?)(\s*), (\s*)(\w*?)(\s*),(\s*)(\w*?)(\s*), (\s*)(\w*?)(\s*),(\s*)(\w*?)(\s*), (\s*)(\w*?)(\s*),(\s*)(\w*?)(\s*), (\s*)(\w*?)(\s*),(\s*)(\w*?)(\s*), (\s*)(\w*?)(\s*),(\s*)(\w*?)(\s*), (\s*)(\w*?)(\s*)$#o; my $s = $11; print $s; } sub splitter { my $s = (split(/\s*,\s*/, $string))[3]; print $s; }

Yields:

[22:31:17] Orders, Master? ./greenhorn-0716.pl Benchmark: timing 100000 iterations of regexpression, splitting... regexpression: 12 wallclock secs (12.78 usr + 0.00 sys = 12.78 CPU) @ + 7824.73/s (n=100000) splitting: 8 wallclock secs ( 7.90 usr + 0.00 sys = 7.90 CPU) @ 12 +658.23/s (n=100000)

Is there something wrong with my code? I know that isn't the best regular expression ever created, but I would like to know why it doesn't beat the split.

Note: To whomever I saw with the shell prompt like I have above, that is so cool!

J. J. Horner
Linux, Perl, Apache, Stronghold, Unix
jhorner@knoxlug.org http://www.knoxlug.org/

Replies are listed 'Best First'.
(chromatic) RE: (jjhorner)Regexp glitch while parsing record in CSV
by chromatic (Archbishop) on Jul 17, 2000 at 07:57 UTC
    Taking out the capturing parenthesis around the whitespace helped a lot. Here's the benchmark results:
    Benchmark: timing 50000 iterations of regex, split... regex: 3 wallclock secs ( 3.03 usr + 0.01 sys = 3.04 CPU) split: 4 wallclock secs ( 3.79 usr + 0.00 sys = 3.79 CPU)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-25 09:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found