diff options
| -rw-r--r-- | challenge-184/solathian/perl/ch1.pl | 58 | ||||
| -rw-r--r-- | challenge-184/solathian/perl/ch2.pl | 82 |
2 files changed, 140 insertions, 0 deletions
diff --git a/challenge-184/solathian/perl/ch1.pl b/challenge-184/solathian/perl/ch1.pl new file mode 100644 index 0000000000..b0b2d805a5 --- /dev/null +++ b/challenge-184/solathian/perl/ch1.pl @@ -0,0 +1,58 @@ +#!usr/bin/perl -w + +#184 Task 1: Sequence Number + +#You are given list of strings in the format aa9999 i.e. first 2 characters can be anything 'a-z' followed by 4 digits '0-9'. + +#Write a script to replace the first two characters with sequence starting with '00', '01', '02' etc. + +use strict; +use warnings; + + +# Example calls + +#my @list = ( 'ab1234', 'cd5678', 'ef1342'); +#Output: ('001234', '015678', '021342') + +#my @list = ( 'pq1122', 'rs3334'); +#Output: ('001122', '013334') + +#sequenceNumber(\@list); + + + + +sub sequenceNumber +{ + my $arrayRef = shift; + my $currentCycle = 0; + + + + foreach my $elem (@$arrayRef) + { + my $tempString = $currentCycle; + + # extend it to two digits + if($currentCycle < 10) + { + $tempString = "0".$currentCycle; + } + + # check if the substitution succeeds + if($elem =~ s/^[a-z]{2}/$tempString/) + { + $currentCycle++; + } + else + { + die"no match in $elem"; + } + + } + + print("(",join(",",@$arrayRef),")\n"); + +} + diff --git a/challenge-184/solathian/perl/ch2.pl b/challenge-184/solathian/perl/ch2.pl new file mode 100644 index 0000000000..937da0d195 --- /dev/null +++ b/challenge-184/solathian/perl/ch2.pl @@ -0,0 +1,82 @@ +#!usr/bin/perl -w + +#184 Task 2: Split Array + +#You are given list of strings containing 0-9 and a-z separated by space only. + +#Write a script to split the data into two arrays, one for integers and one for alphabets only. + +use strict; +use warnings; + +# Example calls + +#my @list = ( 'a 1 2 b 0', '3 c 4 d'); +#Output: [[1,2,0], [3,4]] and [['a','b'], ['c','d']] + +#my @list = ( '1 2', 'p q r', 's 3', '4 5 t'); +#Output: [[1,2], [3], [4,5]] and [['p','q','r'], ['s'], ['t']] + +#splitArray(\@list); + + + + +sub splitArray +{ + my $arrayRef = shift; + my @numberArray; + my @letterArray; + + + + foreach my $elem (@$arrayRef) + { + my @characters = split(" ", $elem); + my @tempNumbers; + my @tempLetters; + + foreach my $singleChar (@characters) + { + if($singleChar =~ /[a-z]{1}/) + { + push(@tempLetters, $singleChar); + } + elsif($singleChar =~ /[0-9]{1}/) + { + push(@tempNumbers, $singleChar); + } + else + { + die "The character is not a number neither a letter." + } + } + + # only push if the array has elements + push(@numberArray, \@tempNumbers) if(@tempNumbers > 0); + push(@letterArray, \@tempLetters) if(@tempLetters > 0); + + + } + + # create output + print("Input:(",join(",",@$arrayRef),")\n["); + + for(my $i=0; $i<@numberArray; $i++) + { + print("[",join(",",@{$numberArray[$i]} ),"]"); + print(",") if($i != (@numberArray -1)); + } + + print("]\n["); + + for(my $i=0; $i<@letterArray; $i++) + { + print("[",join(",",@{$letterArray[$i]} ),"]"); + print(",") if($i != (@letterArray -1)); + } + + print("]\n"); + +} + |
