Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

RE: RE: From one beginner to others . . .

by greenhorn (Sexton)
on Jul 16, 2000 at 01:54 UTC ( #22736=note: print w/replies, xml ) Need Help??


in reply to (Ovid) RE: From one beginner to others . . .
in thread From one beginner to others . . .

I managed to figure out at least one way to use Benchmark, will wonders never cease. :)
(Don't know if I have used it in the best possible way, though.)
Result of running the script shown below:
Benchmark: timing 30 iterations of REGEXP, SPLIT... REGEXP: 10 wallclock secs ( 9.73 usr + 0.25 sys = 9.98 CPU) SPLIT: 47 wallclock secs (47.20 usr + 0.28 sys = 47.48 CPU)

The script didn't pass muster with "-w" when I was trying to print matching lines to "NUL" (file handle hadn't been opened). I changed it simply to count matching lines. "-w" is now happy. (Note to self: something else for later study: how to print only to "NUL" w/out complaint from "-w".)

The source (CSV) file is 13,576 lines long (1,703,397 bytes). Each record has 12 fields; the average length per record is 124 characters. The task is to print only lines whose fourth fields contain "MAPI".

use strict; use Benchmark; timethese( 30, { REGEXP => 'UsingRegExp', SPLIT => 'UsingSplit' } ); sub UsingRegExp { my $file = 'r:\csv\test.csv'; my $field; my $count = 0; open FH, $file or die "\n $file: $!\n"; while ( <FH> ) { # WANT 4TH FIELD. (NOTE: SOME FIELDS _MIGHT_ BE EMPTY.) ($field) = /^[^,]+,[^,]*,[^,]*,\s*([^,]+)\s*,/; $count++ if lc($field) eq "mapi"; # IGNORE CASE } close FH or die "\n $file: $!\n"; } sub UsingSplit { my $file = 'r:\csv\test.csv'; my @record; my $count = 0; open FH, $file or die "\n $file: $!\n"; while ( <FH> ) { @record = split /\s*,\s*/; $count++ if lc($record[3]) eq "mapi"; # IGNORE CASE } close FH or die "\n $file: $!\n"; }

Replies are listed 'Best First'.
RE: From one beginner to others . . .
by frankus (Priest) on Jul 18, 2000 at 17:55 UTC
    I prefer this to whiles:

    <code> map{$r=[split (regex here)];$c+=(lc($r->[9]) eq "mapi")}<FH>; <code>

    I haven't tested this works but I hope you get my drift.

    Brother Frankus.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2022-09-26 20:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (118 votes). Check out past polls.

    Notices?