aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-12-24 19:33:04 +0000
committerGitHub <noreply@github.com>2019-12-24 19:33:04 +0000
commitd1cf0982bdd045cd8508b834740994500efa057d (patch)
treeae67b5e37d45a7bbcd68a59d8c279f2a02cc55d6
parentba19570c2b953edb5816d412e9079ce8ddd9403d (diff)
parent3632c5631d7b94a162df43ff4aed0481ad061e8e (diff)
downloadperlweeklychallenge-club-d1cf0982bdd045cd8508b834740994500efa057d.tar.gz
perlweeklychallenge-club-d1cf0982bdd045cd8508b834740994500efa057d.tar.bz2
perlweeklychallenge-club-d1cf0982bdd045cd8508b834740994500efa057d.zip
Merge pull request #1067 from saiftynet/branch-040
saiftynet's responses for the "Happy Lights" Challenges
-rw-r--r--challenge-040/saiftynet/perl5/ch-1.pl38
-rw-r--r--challenge-040/saiftynet/perl5/ch-2.pl15
2 files changed, 53 insertions, 0 deletions
diff --git a/challenge-040/saiftynet/perl5/ch-1.pl b/challenge-040/saiftynet/perl5/ch-1.pl
new file mode 100644
index 0000000000..a81ab4fa6b
--- /dev/null
+++ b/challenge-040/saiftynet/perl5/ch-1.pl
@@ -0,0 +1,38 @@
+#!/usr/env perl
+# Perl Challenge 040 Task 1
+# Non-destructively print contents of two or more arrays at a given index.
+
+use feature "say";
+my @arraysList=( # array containing 2 or more arrays (as anon array refs)
+ [qw{I L O V E Y O U }],
+ [qw{2 4 0 3 2 0 1 9 }],
+ [qw{! ? £ $ % ^ & * }],
+ );
+
+say "Without an index list";
+printAtIndex(\@arraysList); # call routine with no indices
+
+say "\nWith a single index passed";
+printAtIndex(\@arraysList, 5); # call routine with a single index
+
+say "\nWith a multiple indices passed";
+printAtIndex(\@arraysList, [3,2,1,4]); # call routine with list of indices
+
+sub printAtIndex{
+
+ my ($arrays,$indices)=@_;
+
+ $indices//=[0..$#{@$arrays[0]}]; # if indices not specified make list of all indices
+
+ $indices=[$indices] unless (ref $indices); # if index passed as scalar convert to arrayRef
+
+ foreach my $index (@$indices){ # for each index provided..
+
+ foreach my $array (@arraysList){ # for each array in the list
+
+ print $$array[$index]," "; # print indexed contents of these arrays
+
+ };
+ print "\n"; # next line
+ };
+}
diff --git a/challenge-040/saiftynet/perl5/ch-2.pl b/challenge-040/saiftynet/perl5/ch-2.pl
new file mode 100644
index 0000000000..3b3b4d7c28
--- /dev/null
+++ b/challenge-040/saiftynet/perl5/ch-2.pl
@@ -0,0 +1,15 @@
+#!/usr/env perl
+# Perl Challenge 040 Task 2
+# You are given a list of numbers and set of indices belong to the list.
+# Write a script to sort the values belongs to the indices
+
+use feature 'say';
+
+my @list= ( 10, 4, 1, 8, 12, 3 ); # list for selective sorting
+my @Indices=(0,2,5); # list of indices to sort
+
+say "Original list:- ",join " ",@list; # display original list
+
+@list[@Indices]=sort{$a <=> $b}@list[@Indices]; # sort index values
+
+say "Modified list:- ",join " ",@list; # display results after modification