aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrobbie-hatley <Robbie.Hatley@gmail.com>2023-12-01 19:00:30 -0800
committerrobbie-hatley <Robbie.Hatley@gmail.com>2023-12-01 19:00:30 -0800
commit963eb9901b599278839391b810cb51ad02f368cb (patch)
treeebeaaab4b9e89ba8543f56335b4be0feda68b2a5
parent2ac2c6b618af5515c64bbdd73482636ffe2f79aa (diff)
downloadperlweeklychallenge-club-963eb9901b599278839391b810cb51ad02f368cb.tar.gz
perlweeklychallenge-club-963eb9901b599278839391b810cb51ad02f368cb.tar.bz2
perlweeklychallenge-club-963eb9901b599278839391b810cb51ad02f368cb.zip
Updated 245-1.
-rwxr-xr-xchallenge-245/robbie-hatley/perl/ch-1.pl13
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) . ')';
}