diff options
| author | robbie-hatley <Robbie.Hatley@gmail.com> | 2023-12-01 19:00:30 -0800 |
|---|---|---|
| committer | robbie-hatley <Robbie.Hatley@gmail.com> | 2023-12-01 19:00:30 -0800 |
| commit | 963eb9901b599278839391b810cb51ad02f368cb (patch) | |
| tree | ebeaaab4b9e89ba8543f56335b4be0feda68b2a5 | |
| parent | 2ac2c6b618af5515c64bbdd73482636ffe2f79aa (diff) | |
| download | perlweeklychallenge-club-963eb9901b599278839391b810cb51ad02f368cb.tar.gz perlweeklychallenge-club-963eb9901b599278839391b810cb51ad02f368cb.tar.bz2 perlweeklychallenge-club-963eb9901b599278839391b810cb51ad02f368cb.zip | |
Updated 245-1.
| -rwxr-xr-x | challenge-245/robbie-hatley/perl/ch-1.pl | 13 |
1 files changed, 5 insertions, 8 deletions
diff --git a/challenge-245/robbie-hatley/perl/ch-1.pl b/challenge-245/robbie-hatley/perl/ch-1.pl index 1b3c6a819a..900ec850f9 100755 --- a/challenge-245/robbie-hatley/perl/ch-1.pl +++ b/challenge-245/robbie-hatley/perl/ch-1.pl @@ -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\.[0-9]+$|^-?[1-9]\d*(?:\.[0-9]+)$/; } 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) . ')'; } |
