-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.pl
56 lines (45 loc) · 1.71 KB
/
convert.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/perl -w
#This file will take in a text file from the listserv that is formatted as
#EMAIL {some arbitrary amount of spaces} NAME and convert it into a csv file
#because the listserv is incapable of doing such a simple task for us.
use strict;
#open the files
open SOURCE,"<", "list.txt" or die $!;
open DEST,"+>", "list.csv" or die $!;
#declare varaibles
my ($line,$out,$offset,$name,$firstName,$lastName,$email,$firstSpace,$nextSpace,$lastSpace);
#print out the header line to the csv file
print DEST "\"Last Name\",\"First Name\",\"Email\"\n";
#loop through each line of the rest of the file
while (<SOURCE>) {
chomp;
$line = $_;
#find the first occurance of a space, this will tell us
#when to stop reading the email address
$firstSpace = index($line, " ");
#find the last occurance of a space before it starts printing
#out the name.
#
#this will only give the last name because it finds the last space
#$lastSpace = rindex($line, " ");
$nextSpace = " ";
$lastSpace = -1;
#loop through all of the spaces until the next character is
#not a space, this is where the first name begins
while ($nextSpace eq " ") {
$offset = $lastSpace + 1;
$lastSpace = index($line, " ",$offset);
$nextSpace = substr $line,$lastSpace+1,1;
}
#find the substrings of the email and name and store
#only that into its own stringstring
$email = substr $line, 0, $firstSpace;
$name = substr $line, $lastSpace+1;
$lastSpace = index($name, " ",0);
$firstName = substr $name, 0, $lastSpace;
$lastName = substr $name, $lastSpace+1;
#format out the line that will be inserted into the csv file
$out = sprintf("\"%s\",\"%s\",\"%s\"\n",$lastName,$firstName,$email);
#write it to the file
print DEST $out;
}