diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-10-07 18:38:07 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-10-07 18:38:07 +0100 |
| commit | df1c02585b1d18a91cf6958b38bfec70ea3aea72 (patch) | |
| tree | 1cf3217811c10148d520318354c9de890e5293ea | |
| parent | e153bbd23d15f1b54cc533e4988a617f86a6c02b (diff) | |
| parent | 9848788e0e2e2a7456be99ff1fb40b71cd155f22 (diff) | |
| download | perlweeklychallenge-club-df1c02585b1d18a91cf6958b38bfec70ea3aea72.tar.gz perlweeklychallenge-club-df1c02585b1d18a91cf6958b38bfec70ea3aea72.tar.bz2 perlweeklychallenge-club-df1c02585b1d18a91cf6958b38bfec70ea3aea72.zip | |
Merge pull request #728 from Doomtrain14/master
Added perl5 solution for ch#29-1
| -rw-r--r-- | challenge-029/yet-ebreo/perl5/ch-1.pl | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/challenge-029/yet-ebreo/perl5/ch-1.pl b/challenge-029/yet-ebreo/perl5/ch-1.pl new file mode 100644 index 0000000000..a0392cc0d0 --- /dev/null +++ b/challenge-029/yet-ebreo/perl5/ch-1.pl @@ -0,0 +1,94 @@ +#!/usr/bin/env perl +# Write a script to demonstrate brace expansion. +# For example, script would take command line argument +# Perl {Daily,Weekly,Monthly,Yearly} Challenge and should expand it and print like below: +# Perl Daily Challenge +# Perl Weekly Challenge +# Perl Monthly Challenge +# Perl Yearly Challenge +use strict; +use warnings; +use feature 'say'; + +die "Usage:\n\tch-1.pl <string>\n\n" if @ARGV < 1; +die "Requires exactly one(1) string\n" if @ARGV > 1; +#Note(s): +# - The script needs exactly one string enclosed with "" +# - Unmatched braces will not be expanded +# - The words will be printed in order that they appear inside the {} + +#Remove empty braces before processing using substitution +# The /r returns the result without modifying the $ARGV[0] variable +#Then the resulting string will be used in recusrive function expand +expand($ARGV[0]=~s/{}//gr); + +sub expand { + #The string will be stored in $string + my $string = shift; + + #- Regex was used to check if the string contains matching braces + #- Notice that [^{}]* instead of a simple .* to match the contents of the braces + # this is to make sure that the inner most brace is processed first + #- The matching string was captured using () and will be stored in $1 + if ($string =~ /{([^{}]*)}/) { + # The captured value, the prematch and postmatch were stored + # in variables $l,$m and $r respectively + my ($l,$m,$r) = ($`,$1,$'); + # The captured value stored in $m was split using comma(,) as delimiter + # The resulting list was used in a for loop + for (split ",", $m) { + #A new string containing the prematch, a value from the split operation of $m + #and the postmatch will be used in the recursive function. + #The process will be repeated until... + expand($l.$_.$r); + } + } else { + #The string does not have matching braces. + #Then final string will be printed + say $string; + } +} +=begin +perl ch-1.pl "Perl {Daily,Weekly,Monthly,Yearly} Challenge" +Perl Daily Challenge +Perl Weekly Challenge +Perl Monthly Challenge +Perl Yearly Challenge + +perl ch-1.pl "{The,A} quick {brown,gray} fox jumps over the lazy {dog,cow}" +The quick brown fox jumps over the lazy dog +The quick brown fox jumps over the lazy cow +The quick gray fox jumps over the lazy dog +The quick gray fox jumps over the lazy cow +A quick brown fox jumps over the lazy dog +A quick brown fox jumps over the lazy cow +A quick gray fox jumps over the lazy dog +A quick gray fox jumps over the lazy cow + +-------------------------- +#Some edge/tricky cases +-------------------------- +#No brace +perl ch-1.pl "Perl Challenge" +Perl Challenge + +#Nested braces +perl ch-1.pl "Perl {{Daily,Weekly},Monthly,Yearly} Challenge" +Perl Daily Challenge +Perl Monthly Challenge +Perl Yearly Challenge +Perl Weekly Challenge +Perl Monthly Challenge +Perl Yearly Challenge + +#Unmatched brace +perl ch-1.pl "Perl {daily,monthly Challenge" +Perl {daily,monthly Challenge +perl ch-1.pl "Perl daily,monthly} Challenge" +Perl daily,monthly} Challenge + +#Empty braces +perl .\ch-1.pl "Perl {}Weekly{} Challenges" +Perl Weekly Challenges +=cut + |
