How about this?
use strict; use warnings; use feature 'say'; @ARGV or die 'No range specified'; # Check if the range was specified correctly. # This will filter out negative numbers as well. $ARGV[0] =~ /\A(?:\d+)?-(?:\d+)?\Z/ or die 'Invalid range'; my @commands = qw(zero one two three four); # In case one of the values was not specified it will become an empty +string my ($start, $end) = split '-', $ARGV[0]; # Since an empty string evaluates to false we can assign default value +s like this $start = 0 unless $start; $end = $#commands unless $end; # Negative indices are filtered out by the first regex so we only need + to check if the index is not too big. die "$start is out of range" unless $start < @commands; die "$end is out of range" unless $end < @commands; # The range is sure to be valid now so we can simply use .. to generat +e a list foreach my $index ($start .. $end) { say $commands[$index]; }
In reply to Re: Specifying a range of indices via the command line
by kroach
in thread Specifying a range of indices via the command line
by eyepopslikeamosquito
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |