Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: handler performance question

by ikegami (Patriarch)
on Feb 04, 2009 at 03:12 UTC ( [id://741175]=note: print w/replies, xml ) Need Help??


in reply to handler performance question

By sorting, you can minimize the calls to mkdir, to open and to close. You can even sort such that the order is preserved in each file if so desired.

#!/usr/bin/perl -w use strict; # # Run the script like: # tail -n +2 car.csv | sort | ./foo.pl # # Or if you want to preserve order: # tail -n +2 car.csv | sort -t, -k1,2 -s | ./foo.pl # use File::Path qw( mkpath ); my $out_dir = "/var/tmp/cars"; mkpath($out_dir, 0, 0777) or die; my $last_make = '---'; my $last_model = '---'; my $fh; while (<>) { my ($make,$model) = split(/,/, $_, 4); if ($make ne $last_make) { mkdir("$out_dir/$make", 0777) or die; $last_make = $make; $last_model = '---'; } if ($model ne $last_model) { $fh = undef; open($fh, '>', "$out_dir/$make/$model") or die; $last_model = $model; $fh = undef; } print $fh $_; }

Replies are listed 'Best First'.
Re^2: handler performance question
by hobbs (Monk) on Feb 04, 2009 at 04:00 UTC
    ++ obvious-in-retrospect bright idea :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2024-04-23 21:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found