-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathimportVariable.pl
More file actions
executable file
·73 lines (64 loc) · 2.73 KB
/
importVariable.pl
File metadata and controls
executable file
·73 lines (64 loc) · 2.73 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/perl
use lib $ENV{TDTlite};
require "format.pl";
my $version = "v.51";
my $delim = getDelimiter();
my @fid;
my @args;
my $missingargs = 0;
# needs at least one file name, specified by --files
# accepts either of:
# --files f1,f2,..,fK [assumes that the column to import from each file is column #1;
# column name defaults to file name without path]
# --files f1,f2,..,fK --cols c1,c2,..,cK [imports columns cJ from file fJ]
# --files f1 --cols c1,c2,..,cK [imports columns c1 ... cK from file f1]
# for all options you can specify the columns:
# --colnames cn1,cn2,..,cnK [provides the new variable names; has to match number of columns]
if ($#cols == -1) { for ($i = 0; $i <= $#factorfiles; $i++) { $cols[$i] = 1; } }
if ($#colnames == -1) {
my $i = 0;
foreach $f (@factorfiles) {
$file = substr($f, rindex($f, "/") + 1);
$colnames[$i] = $file;
$i++;
}
}
if ($NUM_factorfiles == 1 && $#cols > 0) { for ($i=0; $i <= $#colnames; $i++) { $factorfiles[$i] = $factorfiles[0]; } }
printVersionHeader("ImportVariable $version");
if ($help) { printHelp("importVariable"); }
elsif ($corpus eq "" || $NUM_factorfiles == 0 || $#cols != $#colnames) { printAbort(); }
else {
my %cases = parseFactorHash();
my $numwarnings = 0;
for ($i=0; $i <= $#colnames; $i++) {
printLine("File: $factorfiles[$i]");
%cases = createFactor($colnames[$i], %cases);
$fid[$i] = getFactorID($colnames[$i], %cases); # ID of variable in the output file (i.e. column)
printLine("$i\tVariable name: $colnames[$i], variable ID: $fid[$i], Column-in-file: $cols[$i]");
open(FILE, $factorfiles[$i]) || die "Couldn't open $factorfiles[$i]\n";
while (<FILE>) {
chop($_);
my ($id, @inFactors) = split($delim, $_);
$id =~ s/^(\d+:\d+)(:00){0,1}/\1/g; # get's rid of excel-caused format mistakes
if (!$cases{$id}) {
if ($warnings) { warn "\t\tWARNING: Illegal case ID: $id \t(item ignored)\n" };
$numwarnings++;
# debugging: print "$id\n";
} # currently processed.
else {
if ($cases{$id}[$fid[$i]] ne emptyValue()) {
if ($warnings && $overwrite) {
warn "\tWARNING: Full cell at Item ID $id\n"; # alarm if the cell is full
warn "\t As requested (-o), cell content ($cases{$id}[$fid[$i]]) will be overwritten.\n\n";
}
elsif ($warnings) { warn "\tWARNING: Full cell at Item ID $id. Value ($cases{$id}[$fid[$i]]) NOT overwritten.\n" };
$numwarnings++;
}
if ($cases{$id}[$fid[$i]] eq emptyValue() || $overwrite) { $cases{$id}[$fid[$i]] = @inFactors[$cols[$i]-1]; }
# debugging: print $cases{$id}[$fid[$i]]."\n";
}
}
}
writeFactorHash(%cases);
}
printFooter();