Skip to content

Instantly share code, notes, and snippets.

@f99aq8ove
Created August 12, 2008 17:43
Show Gist options
  • Save f99aq8ove/5103 to your computer and use it in GitHub Desktop.
Save f99aq8ove/5103 to your computer and use it in GitHub Desktop.
seq implementation in perl
#!/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;
}
#!/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
#!/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';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment