#!/usr/bin/perl -w if (not $ARGV[0] or $ARGV[0] =~ /^-(h|-help)$/ or not -f $ARGV[0]) { print <<'EOF'; # script to transform any CSV file into a CSV file to be imported by jpilot # in the Address Book. # Look for @@@ and modify the script to your needs. # Synopsis: csv2jp (a file jp is created to be imported # into jpilot) EOF exit } use strict; use IO::File; require Text::CSV_XS; my $srccsv = Text::CSV_XS->new({ 'binary' => 1, 'eol' => "\012" }); my $tgtcsv = Text::CSV_XS->new({ 'binary' => 1, 'eol' => "\012", 'always_quote' => 1 }); my $srcfile = new IO::File "$ARGV[0]", "r" || die; my $tgtfile = new IO::File "jp$ARGV[0]", "w" || die; my $srccols; my $tgtcols; ### MAIN ### print $tgtfile "CSV address: Category, Private, Last, First, Title, Company, Phone1, Phone2, Phone3, Phone4, Phone5, Address, City, State, ZipCode, Country, Custom1, Custom2, Custom3, Custom4, Note, phoneLabel1, phoneLabel2, phoneLabel3, phoneLabel4, phoneLabel5, showPhone\n"; $srccols = $srccsv->getline($srcfile); while ( @$srccols ) { print @$srccols,"\n"; $tgtcsv->print($tgtfile, src2tgt($srccols) ) || die; $srccols = $srccsv->getline($srcfile); } exit 0; sub src2tgt { my $mysrc=shift; my @tgt = qw/Category Private Last First Title Company Phone1 Phone2 Phone3 Phone4 Phone5 Address City State ZipCode Country Custom1 Custom2 Custom3 Custom4 Note phoneLabel1 phoneLabel2 phoneLabel3 phoneLabel4 phoneLabel5 showPhone/; # @@@ That's specific to my Palm, change it for you... my %map = ( Custom1 => 'Birthday', Custom2 => 'Department', Custom3 => 'NickName', Custom4 => 'WebPage' ); @tgt = map { $map{$_} or $_ } @tgt; my %tgt; # difference in the array between a phone and it's label my $phone = 6; my $label = 21; my $maxphone = 5; my @tgtlabels = qw/WorkPhone HomePhone Fax OtherPhone Email CompanyPhone Pager Mobile/; my %tgtlabels; # The source array must have header names matching the target array # for the translation to work properly my @src = qw/Last First Title Company WorkPhone Mobile Email State Department Country/; # my @src = qw/Last First Title Company WorkPhone Mobile Email Timezone Organization GeographicalScope/; my (%src,$src); # we transform the source and target arrays in hashes, # makes things clearer. for(my $i=0;$i<@src;$i++) { $src{$src[$i]} = $mysrc->[$i]; } for(my $i=0;$i<@tgt;$i++) { $tgt{$tgt[$i]} = ""; } for(my $i=0;$i<@tgtlabels;$i++) { $tgtlabels{$tgtlabels[$i]} = $i; } ## Now let's do the translation... It's all automatic if headers are ## correctly set... my $currphone = 0; foreach $src (keys %src) { if (defined $tgt{$src}) { $tgt{$src} = $src{$src}; } elsif (defined $tgtlabels{$src}) { if ($currphone < $maxphone) { $tgt{$tgt[$phone+$currphone]} = $src{$src}; $tgt{$tgt[$label+$currphone]} = $tgtlabels{$src}; $currphone++; } } } # the following fields are required (@@@my own little default values): $tgt{"Category"} = "CSV"; $tgt{"Private"} = "0"; $tgt{"showPhone"} = "0"; for(my $i=$currphone; $i < $maxphone; $i++) { $tgt{$tgt[$label+$i]} = "0"; } ## and we transform back the target array... for(my $i=0;$i<@tgt;$i++) { $tgt[$i] = $tgt{$tgt[$i]}; } return \@tgt; }