line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Sort::Sub::by_last_num_in_text; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY |
4
|
|
|
|
|
|
|
our $DATE = '2020-05-25'; # DATE |
5
|
|
|
|
|
|
|
our $DIST = 'Sort-Sub'; # DIST |
6
|
|
|
|
|
|
|
our $VERSION = '0.120'; # VERSION |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
16
|
use 5.010001; |
|
1
|
|
|
|
|
2
|
|
9
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
16
|
|
10
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
74
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub meta { |
13
|
|
|
|
|
|
|
return { |
14
|
0
|
|
|
0
|
0
|
0
|
v => 1, |
15
|
|
|
|
|
|
|
summary => 'Sort by last number found in text or (if no number is found) ascibetically', |
16
|
|
|
|
|
|
|
}; |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub gen_sorter { |
20
|
1
|
|
|
1
|
0
|
2
|
my ($is_reverse, $is_ci) = @_; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub { |
23
|
1
|
|
|
1
|
|
5
|
no strict 'refs'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
235
|
|
24
|
|
|
|
|
|
|
|
25
|
9
|
|
|
9
|
|
12
|
my $caller = caller(); |
26
|
9
|
50
|
|
|
|
14
|
my $a = @_ ? $_[0] : ${"$caller\::a"}; |
|
0
|
|
|
|
|
0
|
|
27
|
9
|
50
|
|
|
|
11
|
my $b = @_ ? $_[1] : ${"$caller\::b"}; |
|
0
|
|
|
|
|
0
|
|
28
|
|
|
|
|
|
|
|
29
|
9
|
|
|
|
|
16
|
my $cmp; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
my @nums; |
32
|
9
|
100
|
|
|
|
0
|
my $num_a; @nums = $a =~ /(\d+)/g; $num_a = $nums[-1] if @nums; |
|
9
|
|
|
|
|
21
|
|
|
9
|
|
|
|
|
14
|
|
33
|
9
|
100
|
|
|
|
12
|
my $num_b; @nums = $b =~ /(\d+)/g; $num_b = $nums[-1] if @nums; |
|
9
|
|
|
|
|
22
|
|
|
9
|
|
|
|
|
15
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
{ |
36
|
9
|
100
|
100
|
|
|
9
|
if (defined $num_a && defined $num_b) { |
|
9
|
100
|
66
|
|
|
29
|
|
|
|
50
|
33
|
|
|
|
|
37
|
6
|
|
|
|
|
12
|
$cmp = $num_a <=> $num_b; |
38
|
6
|
50
|
|
|
|
8
|
last if $cmp; |
39
|
|
|
|
|
|
|
} elsif (defined $num_a && !defined $num_b) { |
40
|
1
|
|
|
|
|
2
|
$cmp = -1; |
41
|
1
|
|
|
|
|
1
|
last; |
42
|
|
|
|
|
|
|
} elsif (!defined $num_a && defined $num_b) { |
43
|
2
|
|
|
|
|
3
|
$cmp = 1; |
44
|
2
|
|
|
|
|
2
|
last; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
0
|
0
|
|
|
|
0
|
if ($is_ci) { |
48
|
0
|
|
|
|
|
0
|
$cmp = lc($a) cmp lc($b); |
49
|
|
|
|
|
|
|
} else { |
50
|
0
|
|
|
|
|
0
|
$cmp = $a cmp $b; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
9
|
50
|
|
|
|
22
|
$is_reverse ? -1*$cmp : $cmp; |
55
|
1
|
|
|
|
|
4
|
}; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
1; |
59
|
|
|
|
|
|
|
# ABSTRACT: Sort by last number found in text or (if no number is found) ascibetically |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
__END__ |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=pod |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=encoding UTF-8 |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=head1 NAME |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
Sort::Sub::by_last_num_in_text - Sort by last number found in text or (if no number is found) ascibetically |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=head1 VERSION |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
This document describes version 0.120 of Sort::Sub::by_last_num_in_text (from Perl distribution Sort-Sub), released on 2020-05-25. |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=head1 SYNOPSIS |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
Generate sorter (accessed as variable) via L<Sort::Sub> import: |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
use Sort::Sub '$by_last_num_in_text'; # use '$by_last_num_in_text<i>' for case-insensitive sorting, '$by_last_num_in_text<r>' for reverse sorting |
80
|
|
|
|
|
|
|
my @sorted = sort $by_last_num_in_text ('item', ...); |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Generate sorter (accessed as subroutine): |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
use Sort::Sub 'by_last_num_in_text<ir>'; |
85
|
|
|
|
|
|
|
my @sorted = sort {by_last_num_in_text} ('item', ...); |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
Generate directly without Sort::Sub: |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
use Sort::Sub::by_last_num_in_text; |
90
|
|
|
|
|
|
|
my $sorter = Sort::Sub::by_last_num_in_text::gen_sorter( |
91
|
|
|
|
|
|
|
ci => 1, # default 0, set 1 to sort case-insensitively |
92
|
|
|
|
|
|
|
reverse => 1, # default 0, set 1 to sort in reverse order |
93
|
|
|
|
|
|
|
); |
94
|
|
|
|
|
|
|
my @sorted = sort $sorter ('item', ...); |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
Use in shell/CLI with L<sortsub> (from L<App::sortsub>): |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
% some-cmd | sortsub by_last_num_in_text |
99
|
|
|
|
|
|
|
% some-cmd | sortsub by_last_num_in_text --ignore-case -r |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head1 DESCRIPTION |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
The generated sort routine will sort by last number (sequence of [0-9]) found in |
104
|
|
|
|
|
|
|
text or (f no number is found in text) ascibetically. Items that have a number |
105
|
|
|
|
|
|
|
will sort before items that do not. |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
This is nifty for something like (L<sortsub> is CLI front-end for L<Sort::Sub>): |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
% ls |
110
|
|
|
|
|
|
|
christophe willem - cafeine (2009)/ |
111
|
|
|
|
|
|
|
christophe willem - inventaire (2007)/ |
112
|
|
|
|
|
|
|
christophe willem - parait-il (2014)/ |
113
|
|
|
|
|
|
|
christophe willem - prismophonic (2011)/ |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
% ls | sortsub last_num_in_text |
116
|
|
|
|
|
|
|
christophe willem - inventaire (2007)/ |
117
|
|
|
|
|
|
|
christophe willem - cafeine (2009)/ |
118
|
|
|
|
|
|
|
christophe willem - prismophonic (2011)/ |
119
|
|
|
|
|
|
|
christophe willem - parait-il (2014)/ |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=for Pod::Coverage ^(gen_sorter|meta)$ |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head1 HOMEPAGE |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
Please visit the project's homepage at L<https://metacpan.org/release/Sort-Sub>. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head1 SOURCE |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Source repository is at L<https://github.com/perlancar/perl-Sort-Sub>. |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head1 BUGS |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Sort-Sub> |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
When submitting a bug or request, please include a test-file or a |
136
|
|
|
|
|
|
|
patch to an existing test-file that illustrates the bug or desired |
137
|
|
|
|
|
|
|
feature. |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head1 SEE ALSO |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
L<Sort::Sub> |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
L<Sort::Sub::by_first_num_in_text> |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head1 AUTHOR |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
perlancar <perlancar@cpan.org> |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
This software is copyright (c) 2020, 2019, 2018, 2016, 2015 by perlancar@cpan.org. |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
154
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=cut |