Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Regexp glitch while parsing record in CSV

by Shoeboy (Sexton)
on Jul 17, 2000 at 07:37 UTC ( #22817=note: print w/replies, xml ) Need Help??


in reply to Regexp glitch while parsing record in CSV

Have you considered optimizing split a little bit. Your regex finds 4 commas and returns whatever's between the 3rd and 4th. Your split finds 11 commas and returns a list of 12 items. It's no suprise that the regex is faster. If you're only trying to return a part of a string, a regex seems to be more appropriate than split. Still, if you like the idea of using split (it is shorter and more readable) -- try limiting split to 5 items like so:
@record = split( /\s*,\s*/o, $_, 5); $field4 = $record[3];
I don't know if split will speed up from this, but it's worth a try.
--Shoeboy
perl -e "do {kill $java, $ada, $cobol, $pascal, $csh;} until die 'Just another perl hacker';"

Replies are listed 'Best First'.
RE: Re: Regexp glitch while parsing record in CSV
by greenhorn (Sexton) on Jul 18, 2000 at 00:50 UTC
    I have found this collection of replies downright fascinating. Bloody regular expressions, anyway. :)

    Given some of the regexp pitfalls that have become obvious as I read through the replies, I find myself thinking that--TMTOWTDI notwithstanding--in this case the "correct" approach might well be the one you've suggested:

    <kbd>@record = split( /\s*,\s*/o, $_, 5);
    $field4 = $record[3];</kbd>

    Judging by q&d benchmarking I did, restricting the split to 5 fields did speed things up a bit. So this approach, while not as sexy as the ones using the regular expressions:
    1) gets the job done;
    2) isn't breathtakingly fast--but isn't dismally slow, either;
    3) is immediately understandable; a complex regexp might lead, later on, to some head-scratching...;
    4) ensures, in a simple way, that the leading and trailing spaces are removed. Once more, thanks to all of you for the extensive feedback.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2019-09-23 19:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The room is dark, and your next move is ...












    Results (284 votes). Check out past polls.

    Notices?