Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Optimizing slow restructuring of delimited files

by periapt (Hermit)
on Jan 25, 2005 at 18:10 UTC ( #424976=note: print w/ replies, xml ) Need Help??


in reply to Optimizing slow restructuring of delimited files

You didn't give much to go on but my first hypothesis would be disk I/O. If the file size is small, you could try assigning the entire file to an array and parsing that. Something like

@filetoread = <INFILE>; # read in file all at once my $linestooutput = ''; # place to save output until the en +d foreach (@filetoread){ @Line = split /\s+/; #split defaults to $_ $linestooutput .= join("\t",@Line[@ColumnNumbers])."\n"; } print OUTFILE $linestooutput; # write output # or even shorter @filetoread = <INFILE>; $linestooutput .= join("\t",(split /\s+/)[@ColumnNumbers])."\n" foreac +h (@filetoread); print OUTFILE $linestooutput;
I'm not sure about the speed impact of interpolated splices? I don't imagine that is the issue but you could try something like this
while(<INFILE>) { # get the current line and split into it's columns @Line = split /\s+/, $_; #print the selected columns to the output my $outline .= @Line[$col]."\t" foreach my $col (@ColumnNumbers); print OUTFILE $outline,"\n"; }


PJ
use strict; use warnings; use diagnostics;


Comment on Re: Optimizing slow restructuring of delimited files
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (14)
As of 2014-09-17 14:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (81 votes), past polls