From 963eb9901b599278839391b810cb51ad02f368cb Mon Sep 17 00:00:00 2001 From: robbie-hatley Date: Fri, 1 Dec 2023 19:00:30 -0800 Subject: Updated 245-1. --- challenge-245/robbie-hatley/perl/ch-1.pl | 13 +++++-------- 1 file 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) . ')'; } -- cgit