Data conversion
Ben Boulanger
ben at blackavar.com
Wed Jan 8 14:07:02 EST 2003
On Wed, 2003-01-08 at 13:05, Travis Roy wrote:
> Let me just add that it's not always 3 fields, it could be up to 30.
> Naming them is not needed.
Alright, then in that case, how about something like this:
#!/usr/bin/perl -w
use strict;
my @lengths;
my $datapos = tell DATA;
while (<DATA>) {
chomp;
my @fields = split(/,/,$_);
for (my $count=0; $count < scalar(@fields); $count++) {
if (! $lengths[$count] || length($fields[$count]) >
$lengths[$count]) {
$lengths[$count] = length($fields[$count]);
}
}
}
#move back to the beginning (or, where ever datapos says to go)
seek(DATA, $datapos, 0);
while (<DATA>) {
chomp;
my @fields = split(/,/, $_);
for (my $count=0; $count < scalar(@fields); $count++) {
printf "%-${lengths[$count]}s ", $fields[$count];
}
print "\n";
}
__END__
travis,roy,email1 at address.com
ben,boulanger,email2 at address.com
georges,foreman,email3 at address.com
More information about the gnhlug-discuss
mailing list