line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::CSV::Field; |
2
|
1
|
|
|
1
|
|
26584
|
use strict; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
42
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
BEGIN { |
5
|
1
|
|
|
1
|
|
565
|
$Text::CSV::Field::VERSION = '1.01'; |
6
|
|
|
|
|
|
|
} |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub new { |
10
|
0
|
|
|
0
|
1
|
|
my ($class,$file,$sep) = @_; |
11
|
|
|
|
|
|
|
|
12
|
0
|
0
|
|
|
|
|
open(FILE,$file) or die "can't open $file $!"; |
13
|
0
|
0
|
|
|
|
|
defined($sep) or die "Missing field separator parameter\n"; |
14
|
0
|
0
|
|
|
|
|
$sep or die "Empty field separator parameter\n"; |
15
|
|
|
|
|
|
|
|
16
|
0
|
|
|
|
|
|
my $self = {_filename=>$file,_sep=>$sep}; |
17
|
0
|
|
|
|
|
|
my @data = (); |
18
|
|
|
|
|
|
|
# open file |
19
|
0
|
|
|
|
|
|
while() { |
20
|
0
|
|
|
|
|
|
chomp; |
21
|
0
|
|
|
|
|
|
push(@data,$_); |
22
|
|
|
|
|
|
|
} |
23
|
0
|
|
|
|
|
|
$self->{_data} = \@data; |
24
|
|
|
|
|
|
|
# check sep |
25
|
|
|
|
|
|
|
|
26
|
0
|
|
|
|
|
|
bless($self,$class); |
27
|
0
|
|
|
|
|
|
return $self; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub check_file { |
31
|
0
|
|
|
0
|
0
|
|
my ($self) = shift; |
32
|
0
|
|
|
|
|
|
my $num_fields = 0; |
33
|
0
|
|
|
|
|
|
my $err_string = undef; |
34
|
0
|
|
|
|
|
|
my @data = @{$self->{_data}}; |
|
0
|
|
|
|
|
|
|
35
|
0
|
0
|
|
|
|
|
if (!@data) { |
36
|
0
|
|
|
|
|
|
$err_string .= "File is empty\n"; |
37
|
0
|
|
|
|
|
|
print "$err_string\n"; |
38
|
0
|
|
|
|
|
|
exit(); |
39
|
|
|
|
|
|
|
} |
40
|
0
|
0
|
|
|
|
|
if (@data == 1) { |
41
|
0
|
|
|
|
|
|
$err_string .= "no data found\n"; |
42
|
0
|
|
|
|
|
|
print "$err_string\n"; |
43
|
0
|
|
|
|
|
|
exit(); |
44
|
|
|
|
|
|
|
} |
45
|
0
|
|
|
|
|
|
for(my $i=0; $i < @data; $i++) { |
46
|
0
|
|
|
|
|
|
my $temp = 0; |
47
|
0
|
|
|
|
|
|
my @temp_arr = split($self->{_sep},${$self->{_data}}[$i]); |
|
0
|
|
|
|
|
|
|
48
|
0
|
|
|
|
|
|
$temp = @temp_arr; |
49
|
0
|
0
|
|
|
|
|
if ($i == 0) { |
50
|
0
|
|
|
|
|
|
$num_fields = $temp; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
else { |
53
|
0
|
0
|
|
|
|
|
if ($num_fields != $temp) { |
54
|
0
|
|
|
|
|
|
$err_string .= "Field number mismatch line# " . ($i+1) . " in " . $self->{_filename} . "\n"; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
} |
58
|
0
|
0
|
|
|
|
|
if ($err_string) { |
59
|
0
|
|
|
|
|
|
print "$err_string\n"; |
60
|
0
|
|
|
|
|
|
exit(); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub setdatafields{ |
65
|
0
|
|
|
0
|
0
|
|
my ($self) = shift; |
66
|
0
|
|
|
|
|
|
$self->check_file(); |
67
|
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
|
$self->{_total_number} = @{$self->{_data}}; |
|
0
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
|
70
|
0
|
|
|
|
|
|
my @fieldnames = (); |
71
|
0
|
|
|
|
|
|
for(my $j = 0; $j < @{$self->{_data}}; $j++) { |
|
0
|
|
|
|
|
|
|
72
|
0
|
|
|
|
|
|
my @recs = split(/$self->{_sep}/,${$self->{_data}}[$j]); |
|
0
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
|
for(my $k = 0; $k < @recs; $k++) { |
74
|
0
|
0
|
|
|
|
|
if ($j == 0) { |
75
|
0
|
|
|
|
|
|
push(@fieldnames,$recs[$k]); |
76
|
0
|
|
|
|
|
|
$self->{$recs[$k]} = []; |
77
|
|
|
|
|
|
|
#push(@{$self->{$fieldnames[$k]}},$recs[$k]); |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
else { |
80
|
0
|
|
|
|
|
|
push(@{$self->{$fieldnames[$k]}},$recs[$k]); |
|
0
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub getfield{ |
87
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
88
|
0
|
|
|
|
|
|
my $field = shift; |
89
|
0
|
0
|
|
|
|
|
$field or die "Missing field input\n"; |
90
|
0
|
|
|
|
|
|
$self->setdatafields; |
91
|
0
|
|
|
|
|
|
$field =~ tr/a-z/A-Z/; |
92
|
0
|
|
|
|
|
|
return @{$self->{$field}}; |
|
0
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
1; |
96
|
|
|
|
|
|
|
__END__ |