line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::TableData::Object::hash; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
13
|
use 5.010001; |
|
1
|
|
|
|
|
2
|
|
4
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
17
|
|
5
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
17
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
273
|
use parent 'Data::TableData::Object::Base'; |
|
1
|
|
|
|
|
200
|
|
|
1
|
|
|
|
|
4
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY |
10
|
|
|
|
|
|
|
our $DATE = '2022-05-19'; # DATE |
11
|
|
|
|
|
|
|
our $DIST = 'Data-TableData-Object'; # DIST |
12
|
|
|
|
|
|
|
our $VERSION = '0.116'; # VERSION |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub new { |
15
|
18
|
|
|
18
|
1
|
24
|
my ($class, $data) = @_; |
16
|
|
|
|
|
|
|
|
17
|
18
|
|
|
|
|
83
|
bless { |
18
|
|
|
|
|
|
|
data => $data, |
19
|
|
|
|
|
|
|
cols_by_name => {key=>0, value=>1}, |
20
|
|
|
|
|
|
|
cols_by_idx => ["key", "value"], |
21
|
|
|
|
|
|
|
}, $class; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub row_count { |
25
|
6
|
|
|
6
|
1
|
9
|
my $self = shift; |
26
|
6
|
|
|
|
|
6
|
scalar keys %{ $self->{data} }; |
|
6
|
|
|
|
|
15
|
|
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub row { |
30
|
6
|
|
|
6
|
1
|
477
|
my ($self, $idx) = @_; |
31
|
|
|
|
|
|
|
# XXX not very efficient |
32
|
6
|
|
|
|
|
11
|
my $rows = $self->rows; |
33
|
6
|
|
|
|
|
23
|
$rows->[$idx]; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub row_as_aos { |
37
|
16
|
|
|
16
|
1
|
492
|
my ($self, $idx) = @_; |
38
|
|
|
|
|
|
|
# XXX not very efficient |
39
|
16
|
|
|
|
|
19
|
my $rows = $self->rows; |
40
|
16
|
|
|
|
|
34
|
$rows->[$idx]; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub row_as_hos { |
44
|
4
|
|
|
4
|
1
|
481
|
my ($self, $idx) = @_; |
45
|
|
|
|
|
|
|
# XXX not very efficient |
46
|
4
|
|
|
|
|
8
|
my $rows = $self->rows; |
47
|
4
|
|
|
|
|
5
|
my $row = $rows->[$idx]; |
48
|
4
|
100
|
|
|
|
9
|
return undef unless $row; ## no critic: Subroutines::ProhibitExplicitReturnUndef |
49
|
3
|
|
|
|
|
13
|
{key => $row->[0], value => $row->[1]}; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub rows { |
53
|
27
|
|
|
27
|
1
|
529
|
my $self = shift; |
54
|
27
|
|
|
|
|
33
|
$self->rows_as_aoaos; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub rows_as_aoaos { |
58
|
28
|
|
|
28
|
1
|
503
|
my $self = shift; |
59
|
28
|
|
|
|
|
33
|
my $data = $self->{data}; |
60
|
28
|
|
|
|
|
73
|
[map {[$_, $data->{$_}]} sort keys %$data]; |
|
82
|
|
|
|
|
149
|
|
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub rows_as_aohos { |
64
|
7
|
|
|
7
|
1
|
479
|
my $self = shift; |
65
|
7
|
|
|
|
|
9
|
my $data = $self->{data}; |
66
|
7
|
|
|
|
|
22
|
[map {{key=>$_, value=>$data->{$_}}} sort keys %$data]; |
|
21
|
|
|
|
|
53
|
|
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub uniq_col_names { |
70
|
6
|
|
|
6
|
1
|
7
|
my $self = shift; |
71
|
|
|
|
|
|
|
|
72
|
6
|
|
|
|
|
8
|
my @res = ('key'); # by definition, hash key is unique |
73
|
6
|
|
|
|
|
6
|
my %mem; |
74
|
6
|
|
|
|
|
6
|
for (values %{$self->{data}}) { |
|
6
|
|
|
|
|
16
|
|
75
|
7
|
100
|
|
|
|
17
|
return @res unless defined; |
76
|
5
|
100
|
|
|
|
15
|
return @res if $mem{$_}++; |
77
|
|
|
|
|
|
|
} |
78
|
3
|
|
|
|
|
4
|
push @res, 'value'; |
79
|
3
|
|
|
|
|
12
|
@res; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub const_col_names { |
83
|
6
|
|
|
6
|
1
|
9
|
my $self = shift; |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# by definition, hash key is not constant |
86
|
6
|
|
|
|
|
7
|
my $i = -1; |
87
|
6
|
|
|
|
|
6
|
my $val; |
88
|
|
|
|
|
|
|
my $val_undef; |
89
|
6
|
|
|
|
|
7
|
for (values %{$self->{data}}) { |
|
6
|
|
|
|
|
13
|
|
90
|
8
|
|
|
|
|
10
|
$i++; |
91
|
8
|
100
|
|
|
|
11
|
if ($i == 0) { |
92
|
5
|
|
|
|
|
5
|
$val = $_; |
93
|
5
|
100
|
|
|
|
12
|
$val_undef = 1 unless defined $val; |
94
|
|
|
|
|
|
|
} else { |
95
|
3
|
100
|
|
|
|
5
|
if ($val_undef) { |
96
|
1
|
50
|
|
|
|
5
|
return () if defined; |
97
|
|
|
|
|
|
|
} else { |
98
|
2
|
50
|
|
|
|
3
|
return () unless defined; |
99
|
2
|
100
|
|
|
|
6
|
return () unless $val eq $_; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
} |
103
|
4
|
|
|
|
|
15
|
('value'); |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub switch_cols { |
107
|
4
|
|
|
4
|
1
|
803
|
die "Cannot switch column in hash table"; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub add_col { |
111
|
0
|
|
|
0
|
1
|
0
|
die "Cannot add_col in hash table"; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub set_col_val { |
115
|
3
|
|
|
3
|
0
|
687
|
my ($self, $name_or_idx, $value_sub) = @_; |
116
|
|
|
|
|
|
|
|
117
|
3
|
|
|
|
|
8
|
my $col_name = $self->col_name($name_or_idx); |
118
|
3
|
|
|
|
|
5
|
my $col_idx = $self->col_idx($name_or_idx); |
119
|
|
|
|
|
|
|
|
120
|
3
|
100
|
|
|
|
12
|
die "Column '$name_or_idx' does not exist" unless defined $col_name; |
121
|
|
|
|
|
|
|
|
122
|
2
|
|
|
|
|
3
|
my $hash = $self->{data}; |
123
|
2
|
100
|
|
|
|
4
|
if ($col_name eq 'key') { |
124
|
1
|
|
|
|
|
1
|
my $row_idx = -1; |
125
|
1
|
|
|
|
|
4
|
for my $key (sort keys %$hash) { |
126
|
3
|
|
|
|
|
4
|
$row_idx++; |
127
|
|
|
|
|
|
|
my $new_key = $value_sub->( |
128
|
|
|
|
|
|
|
table => $self, |
129
|
|
|
|
|
|
|
row_idx => $row_idx, |
130
|
|
|
|
|
|
|
row_name => $key, |
131
|
|
|
|
|
|
|
col_name => $col_name, |
132
|
|
|
|
|
|
|
col_idx => $col_idx, |
133
|
3
|
|
|
|
|
12
|
value => $hash->{$key}, |
134
|
|
|
|
|
|
|
); |
135
|
3
|
50
|
|
|
|
21
|
$hash->{$new_key} = delete $hash->{$key} |
136
|
|
|
|
|
|
|
unless $key eq $new_key; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
} else { |
139
|
1
|
|
|
|
|
2
|
my $row_idx = -1; |
140
|
1
|
|
|
|
|
4
|
for my $key (sort keys %$hash) { |
141
|
3
|
|
|
|
|
5
|
$row_idx++; |
142
|
|
|
|
|
|
|
$hash->{$key} = $value_sub->( |
143
|
|
|
|
|
|
|
table => $self, |
144
|
|
|
|
|
|
|
row_idx => $row_idx, |
145
|
|
|
|
|
|
|
row_name => $key, |
146
|
|
|
|
|
|
|
col_name => $col_name, |
147
|
|
|
|
|
|
|
col_idx => $col_idx, |
148
|
3
|
|
|
|
|
5
|
value => $hash->{$key}, |
149
|
|
|
|
|
|
|
); |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
1; |
155
|
|
|
|
|
|
|
# ABSTRACT: Manipulate hash via table object |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
__END__ |