- #!/usr/bin/perl
- use 5.14.0;
- use warnings;
- use strict;
- # This is a quick and dirty 'seq' knock-off. It'll give you a sequence
- # of numbers or letters (depending on what you put as arguments).
- # The delimiter can be any string or character (even newlines).
- my %alnum;
- # Populate the %alnum hash with keys that correspond with either letters
- # or numbers. This will be used later to translate letters to numbers
- # so we can do math with them. Then we'll translate the numbers back to
- # letters.
- my $n = 0;
- foreach my $letter ('a' .. 'z') {
- $alnum{$letter} = ++$n;
- $alnum{$n} = $letter;
- }
- my $base = $n;
- if (!$ARGV[0]) {
- usage();
- }
- # If either second or third argument is empty, set it to '0'.
- foreach (1 .. 2) {
- if (!$ARGV[$_]) {
- $ARGV[$_] = 0;
- }
- }
- my $delimiter = $ARGV[0];
- my($start, $end) = ($ARGV[1], $ARGV[2]);
- my(@start, @end);
- my $abc_regex = qr/^[a-z]+$/;
- my $n_regex = qr/^[0-9]+$/;
- sub alfa2num {
- # Sort the output so we can easier do calculations with the numbers.
- foreach (@before_start) {
- }
- foreach (@before_end) {
- }
- }
- sub usage {
- say('Usage: ' . basename($0) . " \'[delimiter]\' [start] [end]\n");
- say "* [delimiter] can be any string or character you want.\n" .
- '* [start] and [end] need to be either numeric or alphabetic' .
- " characters.\n";
- exit;
- }
- if ($start eq $end) {
- usage();
- } elsif ($start =~ /$abc_regex/i && $end =~ /$abc_regex/i) {
- alfa2num($start, $end);
- my $start_temp = 0;
- my $end_temp = 0;
- foreach (@start) { $start_temp += $_ };
- foreach (@end) { $end_temp += $_ };
- if ($start_temp == $end_temp) { usage(); }
- elsif ($start_temp > $end_temp) {
- my $diff = ($start_temp - $end_temp);
- while ($start[$_] > 0 && $diff) {
- $start[$_]--;
- $diff--;
- }
- }
- } else {
- my $diff = ($end_temp - $start_temp);
- while ($start[$_] < $base && $diff) {
- $start[$_]++;
- $diff--;
- }
- if ($_ == $last && $diff) {
- }
- }
- }
- exit;
- } elsif ($start =~ /$n_regex/ && $end =~ /$n_regex/) {
- # In case the arguments passed to the script were floating point
- # numbers, round them upwards.
- $start = ceil($start);
- $end = ceil($end);
- if ($start > $end) {
- my $diff = ($start - $end);
- until ($start == $end) {
- $diff--;
- }
- } else {
- my $diff = ($end - $start);
- until ($start == $end) {
- $diff--;
- }
- }
- exit;
- }
- usage();
seq in perl
Posted by Anonymous on Thu 4th Oct 2012 17:25
raw | new post
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.