aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorandrezgz <andrezgz@gmail.com>2019-10-09 08:15:58 -0300
committerandrezgz <andrezgz@gmail.com>2019-10-09 08:15:58 -0300
commitae603a6befbef48b4a3f6fe301bd58bd1dba6e81 (patch)
tree0f1ce67ecd9797445cf8ed5f34a777712f7de0ee
parent83a9aae82c8c36205d38594bc352673058c2d404 (diff)
downloadperlweeklychallenge-club-ae603a6befbef48b4a3f6fe301bd58bd1dba6e81.tar.gz
perlweeklychallenge-club-ae603a6befbef48b4a3f6fe301bd58bd1dba6e81.tar.bz2
perlweeklychallenge-club-ae603a6befbef48b4a3f6fe301bd58bd1dba6e81.zip
challenge-029 andrezgz solution
-rw-r--r--challenge-029/andrezgz/perl5/ch-1.pl56
-rw-r--r--challenge-029/andrezgz/perl5/ch-2.pl24
2 files changed, 80 insertions, 0 deletions
diff --git a/challenge-029/andrezgz/perl5/ch-1.pl b/challenge-029/andrezgz/perl5/ch-1.pl
new file mode 100644
index 0000000000..f8b04fd13b
--- /dev/null
+++ b/challenge-029/andrezgz/perl5/ch-1.pl
@@ -0,0 +1,56 @@
+#!/usr/bin/perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-029/
+# Task #1
+# 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;
+
+sub expand {
+ my $input = shift;
+ my %expansions;
+
+ if ($input =~ m/ (.*?) # optional preamble
+ \{ ([^{}]*?) \} # pair of braces that not contain braces inside
+ (.*) # optional postscript
+ /x ) {
+
+ my ($preamble, $brace_match, $postscript) = ($1,$2,$3);
+ $brace_match = adjust_range($brace_match);
+
+ foreach my $item ( split /,/, $brace_match ) {
+ foreach ( expand( $preamble . $item . $postscript ) ) {
+ $expansions{$_} = 1; #unique expansions
+ }
+ }
+ }
+
+ return %expansions ? sort keys %expansions : ($input);
+}
+
+# replace range with list of values
+sub adjust_range {
+ my $brace_exp = shift;
+ return join ',', ($1..$2) if ( $brace_exp =~ /^(\d+)[.]{2}(\d+)$/ ); # numeric range n..m
+ return join ',', ($1..$2) if ( $brace_exp =~ /^([a-z]+)[.]{2}([a-z]+)$/i ); # alphabetic range a..z
+ return $brace_exp; # unmodified, no range detected
+}
+
+while (<DATA>) {
+ chomp;
+ print "Expansion for: '$_'\n";
+ print join "\n", expand($_);
+ print "\n\n";
+}
+
+__DATA__
+last{mce,boot,xorg}.log
+file.{2015..2017}.{a..c}.log{,.bak}
+/home/tom/{temp,tmp{1..4}}/image.{jpg,png}
+Perl {{Dai,Week,Month,Year}ly,Unique} Challenge
diff --git a/challenge-029/andrezgz/perl5/ch-2.pl b/challenge-029/andrezgz/perl5/ch-2.pl
new file mode 100644
index 0000000000..f131713b81
--- /dev/null
+++ b/challenge-029/andrezgz/perl5/ch-2.pl
@@ -0,0 +1,24 @@
+#!/usr/bin/perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-029/
+# Task #2
+# Write a script to demonstrate calling a C function.
+# It could be any user defined or standard C function.
+
+use strict;
+use warnings;
+
+use Inline C => <<'EOT';
+float square ( float x )
+{
+ return ( x * x );
+}
+EOT
+
+my $input = shift || die "USAGE: $0 <number>";
+print square $input;
+
+__END__
+
+./ch-2.pl 12
+144