diff options
| author | saiftynet <saiftynet@gmail.com> | 2019-12-24 08:14:16 +0000 |
|---|---|---|
| committer | saiftynet <saiftynet@gmail.com> | 2019-12-24 08:14:16 +0000 |
| commit | 950b0a5e36dbe4a0f9c3c6c346a38590eeae1736 (patch) | |
| tree | 10810adceb865b998cfe89f251b61342bbf0f9a4 /challenge-040 | |
| parent | ba19570c2b953edb5816d412e9079ce8ddd9403d (diff) | |
| download | perlweeklychallenge-club-950b0a5e36dbe4a0f9c3c6c346a38590eeae1736.tar.gz perlweeklychallenge-club-950b0a5e36dbe4a0f9c3c6c346a38590eeae1736.tar.bz2 perlweeklychallenge-club-950b0a5e36dbe4a0f9c3c6c346a38590eeae1736.zip | |
saiftynet's responses for the "Happy Lights" Challenges
Diffstat (limited to 'challenge-040')
| -rw-r--r-- | challenge-040/saiftynet/perl5/ch-1.pl | 38 | ||||
| -rw-r--r-- | challenge-040/saiftynet/perl5/ch-2.pl | 15 |
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..b1dbcf1522 --- /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 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 sepcified make list + + $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 |
