aboutsummaryrefslogtreecommitdiff
path: root/challenge-027
diff options
context:
space:
mode:
authorAdam Russell <ac.russell@live.com>2019-09-29 15:58:21 -0400
committerAdam Russell <ac.russell@live.com>2019-09-29 15:58:21 -0400
commit23ae84ea0cdc089535a857109c8f38619d4ffcb9 (patch)
treeeb731c58265fb2ffec2f543b3425b477583337e4 /challenge-027
parentc1d8cb8bebe97b1b1941033d7b04683edc7fb329 (diff)
downloadperlweeklychallenge-club-23ae84ea0cdc089535a857109c8f38619d4ffcb9.tar.gz
perlweeklychallenge-club-23ae84ea0cdc089535a857109c8f38619d4ffcb9.tar.bz2
perlweeklychallenge-club-23ae84ea0cdc089535a857109c8f38619d4ffcb9.zip
solution for challenge 027
Diffstat (limited to 'challenge-027')
-rw-r--r--challenge-027/adam-russell/blog.txt1
-rw-r--r--challenge-027/adam-russell/perl5/Audit.pm19
-rw-r--r--challenge-027/adam-russell/perl5/ch-1.pl31
-rw-r--r--challenge-027/adam-russell/perl5/ch-2.pl21
4 files changed, 66 insertions, 6 deletions
diff --git a/challenge-027/adam-russell/blog.txt b/challenge-027/adam-russell/blog.txt
index e69de29bb2..09b3105dab 100644
--- a/challenge-027/adam-russell/blog.txt
+++ b/challenge-027/adam-russell/blog.txt
@@ -0,0 +1 @@
+https://adamcrussell.livejournal.com/9508.html
diff --git a/challenge-027/adam-russell/perl5/Audit.pm b/challenge-027/adam-russell/perl5/Audit.pm
index 29f0b56a1b..c1d8354213 100644
--- a/challenge-027/adam-russell/perl5/Audit.pm
+++ b/challenge-027/adam-russell/perl5/Audit.pm
@@ -1,30 +1,37 @@
package Audit{
+ use Data::Dump q/pp/;
use Filter::Simple;
our %Log;
sub audit{
my($name, $value) = @_;
- $value = "undef" if !$value;
+ $value = "undef" if !defined($value);
if($Log{$name}){
push @{$Log{$name}}, $value;
}
else{
$Log{$name} = [$value];
}
- #print "A " . $name . " " . $value . "\n";
}
+ sub clear_log{
+ %Log = ();
+ }
+
sub print_log{
for my $key (keys %Log){
print "$key: " . join(", ", @{$Log{$key}}) . "\n";
}
}
+
+ sub pretty_print_log{
+ for my $key (keys %Log){
+ print "$key: " . join(", ", map{ pp $_ } @{$Log{$key}}) . "\n";
+ }
+ }
FILTER_ONLY code => sub{
- s|((\$[[:alpha:]]*).*;)|$1 Audit::audit('$2', $2);|g;
- #print $1 . "\n";
- #print $2 . "\n";
- #print;
+ s|((\$[[:alnum:]_]*).*;)|$1 Audit::audit('$2', $2);|g;
}
}
diff --git a/challenge-027/adam-russell/perl5/ch-1.pl b/challenge-027/adam-russell/perl5/ch-1.pl
index e69de29bb2..f5cdf4895c 100644
--- a/challenge-027/adam-russell/perl5/ch-1.pl
+++ b/challenge-027/adam-russell/perl5/ch-1.pl
@@ -0,0 +1,31 @@
+use strict;
+use warnings;
+##
+# Write a script to find the intersection of two straight lines.
+# The co-ordinates of the two lines should be provided as command line parameters.
+##
+MAIN:{
+ my @endpoints_line_0 = @ARGV[0, 1];
+ my @endpoints_line_1 = @ARGV[2, 3];
+ @endpoints_line_0 = map { $_=~tr/()//d; [split(/,/, $_)];} @endpoints_line_0;
+ @endpoints_line_1 = map { $_=~tr/()//d; [split(/,/, $_)];} @endpoints_line_1;
+ my($a, $b, $c, $d, $p, $q, $r, $s) = (
+ $endpoints_line_0[0]->[0],
+ $endpoints_line_0[0]->[1],
+ $endpoints_line_0[1]->[0],
+ $endpoints_line_0[1]->[1],
+ $endpoints_line_1[0]->[0],
+ $endpoints_line_1[0]->[1],
+ $endpoints_line_1[1]->[0],
+ $endpoints_line_1[1]->[1]
+ );
+ my $denominator = ($c - $a) * ($s - $q) - ($r - $p) * ($c - $b);
+
+ my $x0 = ($c * $b - $a * $d) * ($r - $p) - ($r * $q - $p * $s) * ($c - $a);
+ my $y0 = ($c * $b - $a * $d) * ($s - $q) - ($r * $q - $p * $s) * ($d - $b);
+
+ my $x = $x0/$denominator;
+ my $y = $y0/$denominator;
+
+ print "($a, $b)--($c, $d) and ($p, $q)--($r, $s) intersect at ($x, $y)\n";
+}
diff --git a/challenge-027/adam-russell/perl5/ch-2.pl b/challenge-027/adam-russell/perl5/ch-2.pl
index e69de29bb2..8ebf38a6ee 100644
--- a/challenge-027/adam-russell/perl5/ch-2.pl
+++ b/challenge-027/adam-russell/perl5/ch-2.pl
@@ -0,0 +1,21 @@
+use strict;
+use warnings;
+##
+# Write a script that allows you to capture/display historical data.
+# It could be an object or a scalar.
+##
+use Audit;
+use Class::Hash;
+
+my $test0;
+$test0 = 2;
+$test0 = 3;
+
+Audit::print_log();
+Audit::clear_log();
+
+my $test1 = new Class::Hash(A => "B");
+$test1 = new Class::Hash(B => "C");
+$test1 = new Class::Hash(C => "D");
+#Audit::pretty_print_log();
+Audit::print_log();