f99aq8ove (owner)

Revisions

gist: 5103 Download_button fork
public
Description:
seq implementation in perl
Public Clone URL: git://gist.github.com/5103.git
Embed All Files: show embed
seq.pl #
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
#!/usr/bin/env perl
use strict;
use warnings;
 
my @args = @ARGV;
my %opt;
 
$opt{f} = '%g';
 
foreach (@args) {
    if ($_ =~ /^-(\D)$/) {
        shift @args;
        $opt{$1} = shift @args;
    }
    else {
        last;
    }
}
 
my ($first, $increment, $last) = (1, 1, 1);
if (scalar @args == 1) {
    $last = $args[0];
}
elsif (scalar @args == 2) {
    $first = $args[0];
    $last = $args[1];
}
elsif (scalar @args == 3) {
    $first = $args[0];
    $increment = $args[1];
    $last = $args[2];
}
else {
    die 'invalid argument(s)';
}
 
for (
    my $i = $first;
    ($increment > 0) ? $i <= $last : $i >= $last;
    $i += $increment
    )
{
    printf "$opt{f}\n", $i;
}
 
seq.t #
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env perl
use strict;
use warnings;
use Test::Base;
 
plan tests => 1 * blocks;
 
my $target = $ENV{SEQ_COMMAND};
 
filters {
    input => [qw/chomp make_cmd_str exec_perl_stdout/],
    expected => [qw//],
};
 
sub make_cmd_str {
    return "system '$target @_'";
}
 
run_is_deeply;
 
__END__
 
=== 1 arg (seq 0)
--- input
0
--- expected
 
=== 1 arg (LAST)
--- input
1
--- expected
1
 
=== 1 arg (seq 10)
--- input
10
--- expected
1
2
3
4
5
6
7
8
9
10
 
=== 2 args (FIRST LAST)
--- input
1 10
--- expected
1
2
3
4
5
6
7
8
9
10
 
=== 3 args (FIRST INCREMENT LAST)
--- input
1 2 5
--- expected
1
3
5
 
=== negative start (seq -10 0)
--- input
-10 0
--- expected
-10
-9
-8
-7
-6
-5
-4
-3
-2
-1
0
 
=== null return (seq 10 1)
--- input
10 0
--- expected
 
=== null return (seq -1 -2)
--- input
-1 -2
--- expected
 
=== only 0
--- input
0 0
--- expected
0
 
=== null return (seq -1 -1 1)
--- input
-1 -1 1
--- expected
 
=== 10 to 1
--- input
10 -1 1
--- expected
10
9
8
7
6
5
4
3
2
1
 
=== -1 3 10
--- input
-1 3 10
--- expected
-1
2
5
8
 
=== 10 -4 -2
--- input
10 -4 -2
--- expected
10
6
2
-2
 
=== format
--- input
-f %02g 0 10 100
--- expected
00
10
20
30
40
50
60
70
80
90
100
 
 
seq_error.t #
1
2
3
4
5
6
7
8
9
10
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
 
plan tests => 2;
 
isnt system($ENV{SEQ_COMMAND}), 0, 'no argumant';
isnt system("$ENV{SEQ_COMMAND} 1 1 1 1"), 0, '4 argumants';