Data conversion

Ben Boulanger ben at blackavar.com
Wed Jan 8 13:01:05 EST 2003


On Wed, 2003-01-08 at 12:35, Travis Roy wrote:
> I realize that, but my end solution is a fixed width format. Excel can
> also do that but I was looking for something a little more automated and
> easier. It's a lot easier/faster to take an excel/csv file and type
> convert filename.csv and get a nice fixed width file that I can work
> with.
> 
> Also, some of the files I'm dealing with that are already CSV are
> 800-1000 lines, and Open/StarOffice and Excel have a limit of 65k

Alright, I've been talking to Travis on IM - but maybe I'll re-iterate
it here, as this thread just keeps hammering on and on.

	You have two issues, getting data in and putting data out.

	Putting data out in a formatted setup is easy once you know the max
length of the record.  I understand that you want to figure out the max
length of each entry in each field and then add one - you can do this
one of two ways.  Read the entire thing into memory (does not scale for
large files) or iterate over the file twice and just mark the maximum
length.  Anyone else have a better idea on how to find the max length of
a field in a CSV file?  

	Once you have the data and the max length of each field, simply use
printf to format your output like this:

#!/usr/bin/perl -w
use strict;

#data is in 3 ordered arrays - get the real data in here..
#this is just an example.
my @first = ('first', 'names', 'here');
my @last = ('last', 'names', 'here');
my @emails = ('email', 'addresses', 'here');

# max lengths, compute these some other way
# iterate over the file and length each element.
my $maxleng1 = 5;
my $maxleng2 = 5;
my $maxleng3 = 9;

printf "%${maxleng1}s %${maxleng2}s %${maxleng3}s\n", "First", "Last",
"Email";
printf "%${maxleng1}s %${maxleng2}s %${maxleng3}s\n", "-----", "----",
"-----";

for (my $x = 0; $x < scalar(@first); $x++) {
        printf "%${maxleng1}s %${maxleng2}s %${maxleng3}s\n",
                $first[$x], $last[$x], $emails[$x];
}





More information about the gnhlug-discuss mailing list