line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# Sort::Array.pm |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# Copyright (c) 2001 Michael Diekmann . All rights |
4
|
|
|
|
|
|
|
# reserved. This program is free software; you can redistribute it and/or |
5
|
|
|
|
|
|
|
# modify it under the same terms as Perl itself. |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
# Documentation could be found at the bottom or use (after install): |
8
|
|
|
|
|
|
|
# > perldoc Sort::Array |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
package Sort::Array; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
require 5.003_03; |
13
|
|
|
|
|
|
|
require Exporter; |
14
|
|
|
|
|
|
|
|
15
|
1
|
|
|
1
|
|
601
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
16
|
1
|
|
|
1
|
|
5
|
use vars qw(@EXPORT_OK @ISA $VERSION $error); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
973
|
|
17
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# we export nothing by default :) |
20
|
|
|
|
|
|
|
@EXPORT_OK = qw( |
21
|
|
|
|
|
|
|
Sort_Table |
22
|
|
|
|
|
|
|
Discard_Duplicates |
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
$VERSION = '0.26'; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
#///////////////////////////////////////////////////////////////////////# |
28
|
|
|
|
|
|
|
# # |
29
|
|
|
|
|
|
|
#///////////////////////////////////////////////////////////////////////# |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub Discard_Duplicates { |
32
|
|
|
|
|
|
|
# Get the args and put them into a Hash. |
33
|
0
|
|
|
0
|
0
|
|
my (%arg) = @_; |
34
|
0
|
|
|
|
|
|
$error = 0; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# Check for content that should be sorted, |
37
|
|
|
|
|
|
|
# else return error-code. |
38
|
0
|
0
|
|
|
|
|
if (scalar(@{$arg{data}}) == 0) { |
|
0
|
|
|
|
|
|
|
39
|
0
|
|
|
|
|
|
$error = 104; |
40
|
0
|
|
|
|
|
|
return undef; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
|
my $use_warn = 0; |
44
|
|
|
|
|
|
|
# Turn warnings off, because we do first a '<=>' and if that |
45
|
|
|
|
|
|
|
# fails, we do a 'cmp'. And then a warning comes up. |
46
|
|
|
|
|
|
|
# After working, we turn $^W to the same as before. |
47
|
0
|
0
|
|
|
|
|
if ($^W) { |
48
|
0
|
|
|
|
|
|
$use_warn = $^W; |
49
|
0
|
|
|
|
|
|
$^W = 0; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# Find duplicates and sort them out. |
53
|
0
|
|
|
|
|
|
my %seen = (); |
54
|
0
|
|
|
|
|
|
my @unique = grep { ! $seen{$_}++ } @{$arg{data}}; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
55
|
0
|
|
|
|
|
|
%seen = (); |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
# Check if is set, if empty do not sort them. |
58
|
0
|
0
|
|
|
|
|
if ($arg{sorting} eq 'ascending') { |
|
|
0
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# Sorting content ascending order. |
60
|
0
|
0
|
|
|
|
|
@unique = sort { $a <=> $b || $a cmp $b } @unique; |
|
0
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
elsif ($arg{sorting} eq 'descending') { |
63
|
|
|
|
|
|
|
# Sorting content descending order. |
64
|
0
|
0
|
|
|
|
|
@unique = sort { $b <=> $a || $b cmp $a } @unique; |
|
0
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# Turn warnings to the same as before. |
68
|
0
|
0
|
|
|
|
|
if ($use_warn) { |
69
|
0
|
|
|
|
|
|
$^W = $use_warn; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
# Remove all empty fields, if wished. |
73
|
0
|
0
|
|
|
|
|
if ($arg{empty_fields} eq 'delete') { |
74
|
0
|
|
|
|
|
|
@_ = (); |
75
|
0
|
|
|
|
|
|
foreach (@unique) { |
76
|
0
|
0
|
|
|
|
|
push(@_, $_) if $_; |
77
|
|
|
|
|
|
|
} |
78
|
0
|
|
|
|
|
|
@unique = @_; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
# return @unique; |
81
|
0
|
|
|
|
|
|
@{$arg{data}} = @unique; |
|
0
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
#///////////////////////////////////////////////////////////////////////# |
85
|
|
|
|
|
|
|
# # |
86
|
|
|
|
|
|
|
#///////////////////////////////////////////////////////////////////////# |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub Sort_Table { |
89
|
|
|
|
|
|
|
# Get the args and put them into a Hash. |
90
|
0
|
|
|
0
|
0
|
|
my (%arg) = @_; |
91
|
0
|
|
|
|
|
|
$error = 0; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
# Check if is set, |
94
|
|
|
|
|
|
|
# else return error-code. |
95
|
0
|
0
|
0
|
|
|
|
if ((! $arg{cols}) && ($arg{cols} !~ /0-9/)) { |
96
|
0
|
|
|
|
|
|
$error = 100; |
97
|
0
|
|
|
|
|
|
return undef; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
# Check if is set, |
101
|
|
|
|
|
|
|
# else return error-code. |
102
|
0
|
0
|
0
|
|
|
|
if ((! $arg{field}) && ($arg{field} !~ /0-9/)) { |
103
|
0
|
|
|
|
|
|
$error = 101; |
104
|
0
|
|
|
|
|
|
return undef; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
# Check if is set, |
108
|
|
|
|
|
|
|
# else return error-code. |
109
|
0
|
0
|
0
|
|
|
|
if ((! $arg{sorting}) && (($arg{sorting} ne 'ascending') || ($arg{sorting} ne 'descending'))) { |
|
|
|
0
|
|
|
|
|
110
|
0
|
|
|
|
|
|
$error = 102; |
111
|
0
|
|
|
|
|
|
return undef; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
# Check if set, |
115
|
|
|
|
|
|
|
# else return error-code. |
116
|
0
|
0
|
|
|
|
|
if (! $arg{structure}) { |
117
|
0
|
|
|
|
|
|
$error = 103; |
118
|
0
|
|
|
|
|
|
return undef; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
# Check for content that should be sorted, |
122
|
|
|
|
|
|
|
# else return error-code. |
123
|
0
|
0
|
|
|
|
|
if (scalar(@{$arg{data}}) == 0) { |
|
0
|
|
|
|
|
|
|
124
|
0
|
|
|
|
|
|
$error = 104; |
125
|
0
|
|
|
|
|
|
return undef; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
# Check is set, |
129
|
|
|
|
|
|
|
# else set the standard > ";" |
130
|
0
|
0
|
|
|
|
|
if (! $arg{separator}) { |
131
|
0
|
|
|
|
|
|
$arg{separator} = ';'; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
# Subtract 1 for better readable Arrayfields -> |
135
|
|
|
|
|
|
|
# beginning count at 1 (not 0). ;) |
136
|
0
|
|
|
|
|
|
$arg{cols}--; |
137
|
0
|
|
|
|
|
|
$arg{field}--; |
138
|
|
|
|
|
|
|
|
139
|
0
|
0
|
|
|
|
|
if ($arg{structure} eq 'single') { |
140
|
|
|
|
|
|
|
# Array is not semicolon-separated and we must |
141
|
|
|
|
|
|
|
# convert it to semicolon-separated. |
142
|
0
|
|
|
|
|
|
@_ = (); |
143
|
0
|
|
|
|
|
|
my $i=0; |
144
|
0
|
|
|
|
|
|
while (defined ${$arg{data}}[$i] ne '') { |
|
0
|
|
|
|
|
|
|
145
|
0
|
|
|
|
|
|
my $tmp=''; |
146
|
0
|
|
|
|
|
|
for (0..$arg{cols}) { |
147
|
0
|
|
|
|
|
|
$tmp .= "${$arg{data}}[$i+$_]"; |
|
0
|
|
|
|
|
|
|
148
|
0
|
0
|
|
|
|
|
if ($_ != $arg{cols}) { |
149
|
0
|
|
|
|
|
|
$tmp .= "$arg{separator}"; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
} |
152
|
0
|
|
|
|
|
|
push(@_, $tmp); |
153
|
0
|
|
|
|
|
|
$i += $arg{cols} + 1; |
154
|
|
|
|
|
|
|
} |
155
|
0
|
|
|
|
|
|
@{$arg{data}} = @_; |
|
0
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
0
|
|
|
|
|
|
my $use_warn = 0; |
159
|
|
|
|
|
|
|
# Turn warnings off, because we do first a '<=>' and if that |
160
|
|
|
|
|
|
|
# fails, we do a 'cmp' and then a warning comes up. |
161
|
|
|
|
|
|
|
# After sorting, we turn $^W to the same as before. |
162
|
0
|
0
|
|
|
|
|
if ($^W) { |
163
|
0
|
|
|
|
|
|
$use_warn = $^W; |
164
|
0
|
|
|
|
|
|
$^W = 0; |
165
|
|
|
|
|
|
|
} |
166
|
0
|
0
|
|
|
|
|
if ($arg{sorting} eq 'ascending') { |
|
|
0
|
|
|
|
|
|
167
|
|
|
|
|
|
|
# Sorting content ascending order. |
168
|
0
|
|
|
|
|
|
@{$arg{data}} = |
|
0
|
|
|
|
|
|
|
169
|
0
|
0
|
|
|
|
|
map { $_->[0] } |
170
|
|
|
|
|
|
|
sort { |
171
|
0
|
|
|
|
|
|
$a->[1] <=> $b->[1] |
172
|
|
|
|
|
|
|
|| |
173
|
|
|
|
|
|
|
$a->[1] cmp $b->[1] |
174
|
|
|
|
|
|
|
} |
175
|
0
|
|
|
|
|
|
map { [ $_, (split(/$arg{separator}/))[$arg{field}] ] } |
176
|
0
|
|
|
|
|
|
@{$arg{data}}; |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
elsif ($arg{sorting} eq 'descending') { |
179
|
|
|
|
|
|
|
# Sorting content descending order. |
180
|
0
|
|
|
|
|
|
@{$arg{data}} = |
|
0
|
|
|
|
|
|
|
181
|
0
|
0
|
|
|
|
|
map { $_->[0] } |
182
|
|
|
|
|
|
|
sort { |
183
|
0
|
|
|
|
|
|
$b->[1] <=> $a->[1] |
184
|
|
|
|
|
|
|
|| |
185
|
|
|
|
|
|
|
$b->[1] cmp $a->[1] |
186
|
|
|
|
|
|
|
} |
187
|
0
|
|
|
|
|
|
map { [ $_, (split(/$arg{separator}/))[$arg{field}] ] } |
188
|
0
|
|
|
|
|
|
@{$arg{data}}; |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
# Turn warnings to the same as before. |
192
|
0
|
0
|
|
|
|
|
if ($use_warn) { |
193
|
0
|
|
|
|
|
|
$^W = $use_warn; |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
# Return the sorted Array in the |
197
|
|
|
|
|
|
|
# same format as input. |
198
|
0
|
0
|
|
|
|
|
if ($arg{structure} eq 'csv') { |
|
|
0
|
|
|
|
|
|
199
|
0
|
|
|
|
|
|
return @{$arg{data}}; |
|
0
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
elsif ($arg{structure} eq 'single') { |
202
|
0
|
|
|
|
|
|
@_ = (); |
203
|
0
|
|
|
|
|
|
foreach (@{$arg{data}}) { |
|
0
|
|
|
|
|
|
|
204
|
0
|
|
|
|
|
|
push(@_, split(/$arg{separator}/)); |
205
|
|
|
|
|
|
|
} |
206
|
0
|
|
|
|
|
|
return @_; |
207
|
|
|
|
|
|
|
} |
208
|
|
|
|
|
|
|
} |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
1; |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
__END__ |