in reply to What's the most efficient way to write out many lines of data?

this code is 100% untested, but i think it might do something like what you want. i wish i had more time now, but i'll have to leave it to others to comment if you don't understand. sorry again to dump and run, but i hope this helps.

Update: okay, i found a couple minutes to comment the code and test the script. i've updated the code to a working version, and provided sample input and output. let me know if you have any trouble with this. if the comments aren't enough help, we'll be happy to explain what's going on.

#!/usr/bin/perl use strict; use warnings; use 5.006; $|++; require Text::CSV; ## for writing a proper CSV file ## get arguments from the commandline my( $infile, $outfile )= @_; ## set the template for unpacking the database ## see 'perldoc -f pack' for details my $template= 'A30A30A40'; ## verify arguments or display usage 2 == @ARGV or die "usage: db2csv infile outfile\n"; ## open the files, $infile for reading, $outfile for writing ## see 'perldoc perlopentut' for details open local(*IN) => '<', $infile or die 'infile:', $!; open local(*OUT) => '>', $outfile or die 'outfile:', $!; ## create a CSV object my $csv= Text::CSV->new(); ## set the input record seperator to 100 bytes ## see 'perldoc perlvar' for details local $/= \100; ## process infile a line at a time while( <IN> ) { ## unpack the database record ## and create a csv record $csv->combine( unpack $template => $_ ) or die "cannot create record $.:", $csv->error_input() || $!; ## print the string to the output file print OUT $csv->string(), "\n"; } __END__ sample input: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBBBBBBBBBBCCCCCCCCCC +CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCddddddddddddddddddddddddddddddeeeeeeeee +eeeeeeeeeeeeeeeeeeeeeffffffffffffffffffffffffffffffffffffffff sample output: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA","BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB","CCC +CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC" "dddddddddddddddddddddddddddddd","eeeeeeeeeeeeeeeeeeeeeeeeeeeeee","fff +fffffffffffffffffffffffffffffffffffff"

~Particle *accelerates*

  • Comment on Re: What's the most efficient way to write out many lines of data?
  • Download Code