aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-12-02 11:19:41 +0000
committerGitHub <noreply@github.com>2023-12-02 11:19:41 +0000
commit5362e4ac9bc2f041fff8b95d82ef31e947cb97ab (patch)
tree37be49d0673efc4bc2071f42cacade361f00d766
parentbcb1110fa0e9176152c1e2d87768c2c9d7e926f0 (diff)
parentb141e31cfadd526a3c036527653b1c892ec51b97 (diff)
downloadperlweeklychallenge-club-5362e4ac9bc2f041fff8b95d82ef31e947cb97ab.tar.gz
perlweeklychallenge-club-5362e4ac9bc2f041fff8b95d82ef31e947cb97ab.tar.bz2
perlweeklychallenge-club-5362e4ac9bc2f041fff8b95d82ef31e947cb97ab.zip
Merge pull request #9172 from robbie-hatley/robbie-hatley-245
Updated 245-1.
-rwxr-xr-xchallenge-245/robbie-hatley/perl/ch-1.pl15
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) . ')';
}