diff options
| -rwxr-xr-x | challenge-245/robbie-hatley/perl/ch-1.pl | 15 |
1 files changed, 6 insertions, 9 deletions
diff --git a/challenge-245/robbie-hatley/perl/ch-1.pl b/challenge-245/robbie-hatley/perl/ch-1.pl index 1b3c6a819a..13310e8f45 100755 --- a/challenge-245/robbie-hatley/perl/ch-1.pl +++ b/challenge-245/robbie-hatley/perl/ch-1.pl @@ -42,7 +42,7 @@ IO NOTES: Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a double-quoted array of arrays of two arrays, with the inner array pairs consisting of an array of single-quoted strings followed by an array of small positive integers (1-9), in proper Perl syntax, like so: -./ch-1.pl "([['Go','Lisp','AutoIt3','Logo'],[2, 1, 4, 3]],[['Awk','Cobol','Perl','Sed'],[3,4,1,2]])" +./ch-1.pl "([['Go','Lisp','AutoIt3','Logo'],[2, 19, 111, 3]],[['Awk','Cobol','Perl','Sed'],['golf','quaint','broad','easy']])" Output is to STDOUT and will be each input array followed by the corresponding output. @@ -67,16 +67,18 @@ BEGIN {$t0 = time} # ------------------------------------------------------------------------------------------------------------ # SUBROUTINES: -sub is_array_of_pos_ints($aref) { +sub is_array_of_numbers($aref) { return 0 if 'ARRAY' ne ref $aref; for (@$aref) { - return 0 if !/^[1-9]\d*$/; + return 0 if !/^0$|^-?0\.\d+$|^-?[1-9]\d*(?:\.\d+)*$/; } return 1; } sub sort_array1_by_array2($aref1, $aref2) { - return @$aref1[sort{$$aref2[$a]<=>$$aref2[$b]}0..$#$aref1]; + is_array_of_numbers($aref2) + and return @$aref1[sort{$$aref2[$a]<=>$$aref2[$b]}0..$#$aref1] + or return @$aref1[sort{$$aref2[$a]cmp$$aref2[$b]}0..$#$aref1] } # ------------------------------------------------------------------------------------------------------------ @@ -112,11 +114,6 @@ for my $aref (@arrays) { say 'Moving on to next array.'; next; } - if ( !is_array_of_pos_ints($aref2) ) { - say 'Error: second subarray is not array of positive integers.'; - say 'Moving on to next array.'; - next; - } my @sorted = sort_array1_by_array2($aref1, $aref2); say 'Sorted = (' . join(', ', map {"'$_'"} @sorted) . ')'; } |
