diff options
| author | Burkhard Nickels <chuck@peregrina> | 2019-11-10 18:32:13 +0100 |
|---|---|---|
| committer | Burkhard Nickels <chuck@peregrina> | 2019-11-10 18:32:13 +0100 |
| commit | 4c6d1b6c0d8a3b4b0d9b48cdec0929aca5d239d2 (patch) | |
| tree | 465e9932a6c280c6be06b72a7740795fd392f0c8 /challenge-033 | |
| parent | cd59cfe153f46bc7868e8fda16a217d8908a6b7b (diff) | |
| download | perlweeklychallenge-club-4c6d1b6c0d8a3b4b0d9b48cdec0929aca5d239d2.tar.gz perlweeklychallenge-club-4c6d1b6c0d8a3b4b0d9b48cdec0929aca5d239d2.tar.bz2 perlweeklychallenge-club-4c6d1b6c0d8a3b4b0d9b48cdec0929aca5d239d2.zip | |
Added perl5 solutions challenge 33.
Diffstat (limited to 'challenge-033')
| -rw-r--r-- | challenge-033/burkhard-nickels/blogs.txt | 1 | ||||
| -rwxr-xr-x | challenge-033/burkhard-nickels/perl5/ch-1.pl | 266 | ||||
| -rwxr-xr-x | challenge-033/burkhard-nickels/perl5/ch-1.sh | 3 | ||||
| -rwxr-xr-x | challenge-033/burkhard-nickels/perl5/ch-2.pl | 620 | ||||
| -rw-r--r-- | challenge-033/burkhard-nickels/perl5/example.txt | 1 |
5 files changed, 891 insertions, 0 deletions
diff --git a/challenge-033/burkhard-nickels/blogs.txt b/challenge-033/burkhard-nickels/blogs.txt new file mode 100644 index 0000000000..dea12c1b54 --- /dev/null +++ b/challenge-033/burkhard-nickels/blogs.txt @@ -0,0 +1 @@ +pearls.dyndnss.net diff --git a/challenge-033/burkhard-nickels/perl5/ch-1.pl b/challenge-033/burkhard-nickels/perl5/ch-1.pl new file mode 100755 index 0000000000..9ee8ffb1d4 --- /dev/null +++ b/challenge-033/burkhard-nickels/perl5/ch-1.pl @@ -0,0 +1,266 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use Getopt::Long; +use IO::All; +use Data::Dumper qw(Dumper); + +=head1 Perl Weekly Challenge #33 Task #1: Count letters + +This week PWC in Task #1 shall "Count letters" from sentences in one or +more files. + +For counting the letters I chose the perl module IO::All to read the whole +files, filtering with grep and split and using for/foreach loops for +printing the results. And I created a solution as one-liner that uses +the C<-F> option to already split the input. + +=begin html + +<h2> Download and References </h2> +<b>Download File:</b><a href="ch-1.pl" download>Solution PWC #33 Task #1 ch-1.pl</a><br> +<br><br> + +=end html + +=head1 SYNOPSIS + + # perldoc ch-1.pl - POD + # ./ch-1.pl html - HTML/CSS in ch-1.html/pwc.css + # ./ch-1.pl help - Usage information + + ./ch-1.pl [<command>] [<files>] + + # ./ch-1.pl one example.txt + # ./ch-1.pl task1 example.txt + +=cut + +# ====================== TASK 1 ============================== + +=head1 Definition Task #1: Count Letters (A..Z) + +Create a script that accepts one or more files specified on the command-line and count the number of times letters appeared in the files. + +So with the following input file sample.txt + + The quick brown fox jumps over the lazy dog. + +the script would display something like: + + a: 1 + b: 1 + c: 1 + d: 1 + e: 3 + f: 1 + g: 1 + h: 2 + i: 1 + j: 1 + k: 1 + l: 1 + m: 1 + n: 1 + o: 4 + p: 1 + q: 1 + r: 2 + s: 1 + t: 2 + u: 2 + v: 1 + w: 1 + x: 1 + y: 1 + z: 1 + +=cut + +# ====================== TASK 1 ============================== + +=head1 Solution Task #1: Count letters + +In line 2 the C<IO::All slurp()> function reads the files that are +given in C<@ARGV>. Line 5 is doing a C<split> on every character and +the C<grep> on letters that are than stored in C<@items>. The C<foreach> +loop in line 9 increases a counter for every lower case, C<lc()>, +letter. In the one-liner this is done via the C<map()> function. +The result is printed in a C<foreach> loop from line 12 to 14. + + 1 my $lines; + 2 foreach(@ARGV) { $lines .= io($_)->slurp; } + 3 + 4 # ---------- Input and grep letters --------- + 5 my @items = grep { /[a-zA-Z]/ } split //,$lines; + 6 + 7 # ---------- Count letters ------------ + 8 my %sum; + 9 foreach my $i (@items) { $sum{lc($i)}++; } + 10 + 11 # -------- Output -------------- + 12 foreach my $i (sort keys %sum) { + 13 print "$i: $sum{$i}\n"; + 14 } + +=cut + +sub task1 { + my $lines; + foreach(@ARGV) { $lines .= io($_)->slurp; } + + # ---------- Input and grep letters --------- + my @items = grep { /[a-zA-Z]/ } split //,$lines; + + # ---------- Count letters ------------ + my %sum; + foreach my $i (@items) { $sum{lc($i)}++; } + + # -------- Output -------------- + foreach my $i (sort keys %sum) { + print "$i: $sum{$i}\n"; + } +} + +=head2 One-Liner + +I also tried to create the whole script in one line. The C<-F//> Option +defines a character to split the input. I used two C<-e> Options, which +each defines a line of perl code. + +On the first line the splitted string +is stored in the list C<@F>. With C<grep> are filtered all letters and +mapped into the hash C<%s>. + +On the second line a C<for> loop prints the result. In file F<example.txt> +is the sentence from Task #1. When two the same files are the input, the +values are the double. + + # ./pwc33.pl one example.txt example.txt + pwc33.pl (Version 1.0): One-Liner + One liner: example.txt example.txt + Command: perl -F// -e 'map { $_ => ++$s{lc($_)} } grep {/[a-zA-Z]/} @F;' -e 'END{ for(keys %s) { print "$_:$s{$_}\n";} }' example.txt example.txt + y:2 + z:2 + x:2 + p:2 + d:2 + a:2 + v:2 + T:2 + n:2 + w:2 + u:4 + c:2 + g:2 + e:6 + f:2 + l:2 + o:8 + q:2 + m:2 + h:4 + r:4 + k:2 + i:2 + t:2 + b:2 + s:2 + j:2 + +=cut + + +sub task1_one_liner { + my $file = join(" ",@ARGV); + print "One liner: $file\n"; + my $cmd = 'perl -F// -e \'map { $_ => ++$s{$_} } grep {/[a-zA-Z]/} @F;\' -e \'END{ for(keys %s) { print "$_:$s{$_}\n";} }\''; + + print "Command: $cmd $file\n"; + system( "$cmd $file" ); +} + +# ----------------- Global Vars ----------------- + +# ----------------- Main program ----------------- +print "ch-1.pl (Version 1.0) PWC #33 Task #1: "; + +my $command = shift @ARGV; # Read command + +# -- Verify file for commands "one", "task1" +if($command and ($command eq "one" or $command eq "task1")) { + if(! $ARGV[0]) { die "Error: Missing file name!\n"; } + if(! -f $ARGV[0]) { die "Error: File does not exist $ARGV[0]!\n"; } +} + +# -- Execution of simple commands ----------------- +if(! $command) { # if no command print usage + print "Usage\n"; + usage(); +} +elsif($command eq "html") { # if command html or help + print "HTML\n"; + html(); +} +elsif($command eq "help") { + print "Help\n"; + usage(); +} +elsif($command eq "one") { + print "One-Liner\n"; + task1_one_liner(); +} +elsif($command eq "task1") { + print "Task #1\n"; + task1(); +} + +# ================================ Usage ============================ +sub usage { + print "./ch-1.pl [<command>] [<files>]\n"; + print "\n"; + print " command, help|html|task1|one \n"; + print " help, Prints out some usage information.\n"; + print " html, Writes HTML and CSS from POD.\n"; + print " task1, Solution for Task #1.\n"; + print " one, Solution for Task #1 as One-Liner.\n"; + print " files, list of filenames.\n"; + print "\n"; + print " Examples:\n"; + print " # perldoc ch-1.pl\n"; + print " # ./ch-1.pl help\n"; + print " # ./ch-1.pl html\n"; + print " # ./ch-1.pl one example.txt\n"; + print " # ./ch-1.pl task1 example.txt\n"; +} + +sub html { + # ------------- Create HTML -------------- + qx[ pod2html --header --title \"Perl Weekly Challenge #33 Task #1, Count Letters\" --css \"pwc.css\" ch-1.pl > ch-1.html ]; + + # ------------- Create CSS -------------- + my $CSS =<<CSS; +body { margin-left:auto; margin-right:auto; width:80%; } +h1 { border-bottom:4px solid red; } +h2 { border-bottom:2px solid orange; } +pre { border:2px solid grey; background-color:#eef; padding:10px; } +li { padding:5px; } +a { text-decoration:none; color:black; padding:4px; } +a:hover { background-color: brown; color:white; } +._podblock_ { width:100%; background-color:black; color:white; padding:10px; } +CSS + + open(CSS, ">pwc.css") or die "Cant open pwc.css!\n"; + print CSS $CSS; + close CSS; +} + +=head1 AUTHOR + +Chuck + +=cut + +# ############################## END ############################################# + diff --git a/challenge-033/burkhard-nickels/perl5/ch-1.sh b/challenge-033/burkhard-nickels/perl5/ch-1.sh new file mode 100755 index 0000000000..623b00ee2f --- /dev/null +++ b/challenge-033/burkhard-nickels/perl5/ch-1.sh @@ -0,0 +1,3 @@ + +perl -F// -e 'map { $_ => ++$s{$_} } grep {/[a-zA-Z]/} @F;' -e 'END{ for(keys %s) { print "$_:$s{$_}\n";} }' example.txt example.txt + diff --git a/challenge-033/burkhard-nickels/perl5/ch-2.pl b/challenge-033/burkhard-nickels/perl5/ch-2.pl new file mode 100755 index 0000000000..317a2bdfa1 --- /dev/null +++ b/challenge-033/burkhard-nickels/perl5/ch-2.pl @@ -0,0 +1,620 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use Getopt::Long; +use PDL; +use PDL::NiceSlice; +use Data::Dumper qw(Dumper); + +=head1 Perl Weekly Challenge #33 Task #2: Multiplication Table + +This week PWC in Task #2 shall print a multiplication table. + +For the multiplication table I chose a more difficult approach with Perl +Data Language (PDL) to do the calculation of the table and the Perl format +function for printing the results. + +=begin html + +<h2> Download and References </h2> +<b>Download File:</b><a href="ch-2.pl" download>Solution PWC #33 Task #2 ch-2.pl</a><br> +<br><br> +<a target=_blank href="http://pdl.perl.org/content/pdl-book-toc.html">pdl.perl.org: PDL Book</a><br> +<br> +<a target=_blank href="http://pdl.perl.org/">pdl.perl.org</a><br> +<br> + +=end html + +=head1 SYNOPSIS + + # perldoc ch-2.pl - POD + # ./ch-2.pl html - HTML/CSS in ch-2.html/pwc.css + # ./ch-2.pl help - Usage information + + ./ch-2.pl <command> [<options>] + + # perldoc ch-2.pl + # ./ch-2.pl help + # ./ch-2.pl html + # ./ch-2.pl task2 + # ./ch-2.pl task2 -p + # ./ch-2.pl task2 --pdl + # ./ch-2.pl task2 --array + # ./ch-2.pl task2 --simple + # ./ch-2.pl task2 --slice + +=cut + +# ====================== TASK 2 ============================== + +=head1 Definition Task #2: Formatted Multiplication Table + +Write a script to print 11x11 multiplication table, only the top half triangle. + + x| 1 2 3 4 5 6 7 8 9 10 11 +---+-------------------------------------------- + 1| 1 2 3 4 5 6 7 8 9 10 11 + 2| 4 6 8 10 12 14 16 18 20 22 + 3| 9 12 15 18 21 24 27 30 33 + 4| 16 20 24 28 32 36 40 44 + 5| 25 30 35 40 45 50 55 + 6| 36 42 48 54 60 66 + 7| 49 56 63 70 77 + 8| 64 72 80 88 + 9| 81 90 99 + 10| 100 110 + 11| 121 + +=cut + +# ====================== Solution TASK 2 ============================== + +sub task2_pdl { + my ($n,$e) = (12,1); + my $a = xvals($n,$n) * yvals($n,$n); # Calculate the Matrix. + foreach my $y (2..11) { + $a(1:$e++,($y)) .= 0; # Set "0" Zero to one Half. + } + output_pdl($a); # Output of Matrix. +} + +=head1 Solution Task #2: PDL Perl Data Language + +First I tried a not so easy solution for a rather easy task. Because PDL, +the Perl Data Language, needs a lot effort to understand +before you can use it. PDL is more meant for big matrix calculations. + +PDL allows you to do matrix calcuations in a rather +fast way. Matrix can also for example mean Image Processing. And for Image +Processing some performance is needed. + +But I wanted to try it here, to recall some knowledge about PDL. +So the resulting calculation of the Matrix looks small and easy: + + use PDL; # Include PDL Module. + my ($n,$e) = (12,1); + my $a = xvals($n,$n) * yvals($n,$n); # Calculate the Matrix. + foreach my $y (2..11) { + $a(1:$e++,($y)) .= 0; # Set "0" Zero to one Half. + } + output_pdl($a); # Output of Matrix. + +=head2 PDL xvals() / yvals() + +With the PDL function xvals() you can create a matrix with X index values. +The Index of a Matrix/Array goes from 0 to N. And this Index you can create +in a 2-dimensional Matrix in several rows. In our example we create a +12x12 Matrix of the X and Y Index. + +Create an example Index Matrix with xvals(): + + perl -e 'use PDL; print xvals(12,2);' + [ + [ 0 1 2 3 4 5 6 7 8 9 10 11] + [ 0 1 2 3 4 5 6 7 8 9 10 11] + ] + +Create an example Index Matrix with yvals(): + + perl -e 'use PDL; print yvals(2,12);' + + [ + [ 0 0] + [ 1 1] + [ 2 2] + [ 3 3] + [ 4 4] + [ 5 5] + [ 6 6] + [ 7 7] + [ 8 8] + [ 9 9] + [10 10] + [11 11] + ] + +So we create our two index matrices 12x12, because we need values +from 0 to 11: + + my $xi = xvals(12,12); + my $yi = yvals(12,12); + +More details on the xvals() and yvals() functions can be found in: + + perldoc PDL::Basics + +=head2 PDL Multiplication + +Now we do a multiplication of the single values of these two matrices. +When you work with PDL, the objects you work with are called Piddles. +These Piddles (PDL Matrices) can be used in several types of operations. +So you can also use simple Matrix operations on them. + +The xvals()/yvals() functions return a piddle object. The multiplication +of them returns another piddle object. A piddle object can be printed +simply with a print statement. The result below contains already the +data we wanted to have. Only some extra values are disturbing a little bit. + + my $a = xvals($n,$n) * yvals($n,$n); # Calculate the Matrix. + print $a; + + [ + [ 0 0 0 0 0 0 0 0 0 0 0 0] + [ 0 1 2 3 4 5 6 7 8 9 10 11] + [ 0 2 4 6 8 10 12 14 16 18 20 22] + [ 0 3 6 9 12 15 18 21 24 27 30 33] + [ 0 4 8 12 16 20 24 28 32 36 40 44] + [ 0 5 10 15 20 25 30 35 40 45 50 55] + [ 0 6 12 18 24 30 36 42 48 54 60 66] + [ 0 7 14 21 28 35 42 49 56 63 70 77] + [ 0 8 16 24 32 40 48 56 64 72 80 88] + [ 0 9 18 27 36 45 54 63 72 81 90 99] + [ 0 10 20 30 40 50 60 70 80 90 100 110] + [ 0 11 22 33 44 55 66 77 88 99 110 121] + ] + +=head2 PDL Slices + +PDL has an easy way to view only a part of your Matrix. +Details on this are documented in C<perldoc PDL::NiceSlice>. +You can create a slice of that Matrix with typing the +wanted index. This Slice is not a copy of your Matrix, only +a different view, so you need to know that you are working on +your original data. + + my $b = $a(1:11,1:11); + print $b; + + [ + [ 1 2 3 4 5 6 7 8 9 10 11] + [ 2 4 6 8 10 12 14 16 18 20 22] + [ 3 6 9 12 15 18 21 24 27 30 33] + [ 4 8 12 16 20 24 28 32 36 40 44] + [ 5 10 15 20 25 30 35 40 45 50 55] + [ 6 12 18 24 30 36 42 48 54 60 66] + [ 7 14 21 28 35 42 49 56 63 70 77] + [ 8 16 24 32 40 48 56 64 72 80 88] + [ 9 18 27 36 45 54 63 72 81 90 99] + [ 10 20 30 40 50 60 70 80 90 100 110] + [ 11 22 33 44 55 66 77 88 99 110 121] + ] + +=head2 Setting Values with Slices. + +So with this slicing feature we can set also values to these Slices. +We use it to set our not wanted values to Zero. In our solution we +work on the original Matrix, not on the above extract that already +excluded row one and column 0 (that has only zeroes). So we start +on Index 2. Means in our first iteration we call + + $a(1:1,(2)); + +We take the whole row 2 "(2)" with column from index 1 to index 1. +We change only one value to Zero. And in our second iteration we call + + $a(1:2,(3)); + +Means in row 3 we change 2 values to Zero. + + foreach my $y (2..11) { + $a(1:$e++,($y)) .= 0; # Set "0" Zero to one Half. + } + + [ + [ 0 0 0 0 0 0 0 0 0 0 0 0] + [ 0 1 2 3 4 5 6 7 8 9 10 11] + [ 0 0 4 6 8 10 12 14 16 18 20 22] + [ 0 0 0 9 12 15 18 21 24 27 30 33] + [ 0 0 0 0 16 20 24 28 32 36 40 44] + [ 0 0 0 0 0 25 30 35 40 45 50 55] + [ 0 0 0 0 0 0 36 42 48 54 60 66] + [ 0 0 0 0 0 0 0 49 56 63 70 77] + [ 0 0 0 0 0 0 0 0 64 72 80 88] + [ 0 0 0 0 0 0 0 0 0 81 90 99] + [ 0 0 0 0 0 0 0 0 0 0 100 110] + [ 0 0 0 0 0 0 0 0 0 0 0 121] + ] + +Now we have set all our values properly, only we have some extra Zero +values in our Matrix. We saw already that we can exclude row 1 and +column 1 with our Slicing feature and could simply print it. + +But Task #2 requires some special output formatting. This we will +do in a second step now. + +=cut + +# ------------------------------------------------------------------ + +sub output_pdl { # Print PDL piddle + my ($pdl) = @_; # Input parameter is our Matrix $pdl + my ($i,@a); # Declaration of our 2 format Vars. + +# ----- format function for output +format STDOUT_TOP = + x| 1 2 3 4 5 6 7 8 9 10 11 + ---+-------------------------------------------- +. +format STDOUT = + @>>|@>>>@>>>@>>>@>>>@>>>@>>>@>>>@>>>@>>>@>>>@>>> + $i, @a +. + + foreach $i (1..11) { # iterate each row + my @tmp = list $pdl(1:11,($i)); # extrace row piddle and convert to perl list. + @a = map { $_==0?(" "):($_) } @tmp; # set zero values to empty string. + write; # write format to stdout. + } +} + +=head2 Output of PDL Piddle with perl format + +For the printing of the result we use the Perl format function. +Again a little bit more effort is needed to understand it than to +simply use a C<print> or C<printf>, but not everything shall be easy. +The explanation of format is in C<perldoc -f format> or C<perldoc perlform>. + +And we need the PDL Slicing again and also the PDL list() function to +create a perl array/list for printing. + +The output_pdl() function looks like this: + + sub output_pdl { # Print PDL piddle + my ($pdl) = @_; # Input parameter is our Matrix $pdl + my ($i,@a); # Declaration of our 2 format Vars. + + # ----- format function for output + format STDOUT_TOP = + x| 1 2 3 4 5 6 7 8 9 10 11 + ---+-------------------------------------------- + . + format STDOUT = + @>>|@>>>@>>>@>>>@>>>@>>>@>>>@>>>@>>>@>>>@>>>@>>> + $i, @a + . + + foreach $i (1..11) { # iterate each row + my @tmp = list $pdl(1:11,($i)); # extrace row piddle and convert to perl list. + @a = map { $_==0?(" "):($_) } @tmp; # set zero values to empty string. + write; # write format to stdout. + } + } + +The format is defined in STDOUT_TOP, simply a string that is printed as header. +And format STDOUT with the Vars $i and @a, that is magically printing each line +to stdout with the C<write> statement. + +Here we use the PDL Slicing feature to create each line: + + $pdl(1:11,($i)); + +And the PDL list function to convert it to a perl array: + + list $pdl(1:11,($i)); + +Afterwards we C<map> each Zero value to an empty string: + + @a = map { $_==0?(" "):($_) } @tmp; + +So the resulting output looks exactly what was required in Task #2: + + x| 1 2 3 4 5 6 7 8 9 10 11 + ---+-------------------------------------------- + 1| 1 2 3 4 5 6 7 8 9 10 11 + 2| 4 6 8 10 12 14 16 18 20 22 + 3| 9 12 15 18 21 24 27 30 33 + 4| 16 20 24 28 32 36 40 44 + 5| 25 30 35 40 45 50 55 + 6| 36 42 48 54 60 66 + 7| 49 56 63 70 77 + 8| 64 72 80 88 + 9| 81 90 99 + 10| 100 110 + 11| 121 + +=cut + +# ------------------------------------------------------------------ + +=head1 Task #2: Some other Solutions + +I created some other functions that solves Task #2 slightly different. + +=over 3 + +=item * task2_slice() + is printing the intermediate results of the + multiplication, the slicing, the slices with zero values and + the final result. + +=item * task2_simple() + is the most obvious solution that creates the matrix with + two for loops and immediately prints the result. + +=item * task2_create_array() + is first creating an array and then using the function output_array() + to print the array. + +=back + +=head2 task2_slice() + +All the theory behing this code has already be explained in the previous chapters. + + sub task2_slice { + my $n = 12; + my $a = xvals($n,$n) * yvals($n,$n); + print $a; + my $b = $a(1:11,1:11); + print $b; + + my $e = 0; + foreach my $y (1..10) { $b(0:$e++,($y)) .= 0; } + print $b; + output_pdl($a); + } + +=cut + +sub task2_slice { + my $n = 12; + my $a = xvals($n,$n) * yvals($n,$n); + print $a; + my $b = $a(1:11,1:11); + print $b; + + my $e = 0; + foreach my $y (1..10) { $b(0:$e++,($y)) .= 0; } + print $b; + output_pdl($a); +} + +=head2 task2_simple() + +In Task #2 Simple the whole task is solved in a rather simple perl way +with C<for> and C<printf>. + + sub task2_simple { + print " x|"; for(1..11) { printf("%4s",$_); } print "\n"; + print "----+"; for(1..11) { printf("%4s","----"); } print "\n"; + + my $s = 1; + for(my $x=1;$x<=11;$x++) { + printf("%4s|",$x); + for(my $y=1;$y<=11;$y++) { + my $v = " "; + if($y >= $s) { $v = $x * $y; } + printf("%4s", $v); + } + $s++; + print "\n"; + } + } + +=cut + +sub task2_simple { + print " x|"; for(1..11) { printf("%4s",$_); } print "\n"; + print "----+"; for(1..11) { printf("%4s","----"); } print "\n"; + + my $s = 1; + for(my $x=1;$x<=11;$x++) { + printf("%4s|",$x); + for(my $y=1;$y<=11;$y++) { + my $v = " "; + if($y >= $s) { $v = $x * $y; } + printf("%4s", $v); + } + $s++; + print "\n"; + } +} + +=head2 task2_create_array() and output_array() + +This code I show here also without any further explanatios. +Only the array is calculated in the function C<task2_create_array()> +and returned as an array ref. So it can be further used. +The result is printed in C<output_array($aref)>. The function +takes the before created C<$aref>. + + sub task2_create_array { + my @a; + my $s = 0; # Counter to determine Zero Values + for(my $x=0;$x<=10;$x++) { # X-Value + for(my $y=0;$y<=10;$y++) { # Y-Value + my $v = 0; # Resulting Multiplication + if($y >= $s) { # Determine Zero Values + $v = ($x+1)*($y+1);# Multiplication + } + $a[$x][$y] = $v; # Store Value in Array + } + $s++; # Increase of Non-Valid Counter + } + return \@a; + } + + sub output_array { + my ($aref) = @_; + print " x|"; for(1..11) { printf("%4s",$_); } print "\n"; + print "----+"; for(1..11) { printf("%4s","----"); } print "\n"; + + my $row = 1; + foreach my $x (@$aref) { + printf("%4s|",$row++); + foreach my $y (@$x) { + my $v = " "; + if($y) { $v = $y; } + printf("%4s", $v); + } + print "\n"; + } + } + +=cut + +sub task2_create_array { + my @a; + my $s = 0; # Counter to determine Zero Values + for(my $x=0;$x<=10;$x++) { # X-Value + for(my $y=0;$y<=10;$y++) { # Y-Value + my $v = 0; # Resulting Multiplication + if($y >= $s) { # Determine Zero Values + $v = ($x+1)*($y+1);# Multiplication + } + $a[$x][$y] = $v; # Store Value in Array + } + $s++; # Increase of Non-Valid Counter + } + return \@a; +} + +sub output_array { + my ($aref) = @_; + print " x|"; for(1..11) { printf("%4s",$_); } print "\n"; + print "----+"; for(1..11) { printf("%4s","----"); } print "\n"; + + my $row = 1; + foreach my $x (@$aref) { + printf("%4s|",$row++); + foreach my $y (@$x) { + my $v = " "; + if($y) { $v = $y; } + printf("%4s", $v); + } + print "\n"; + } +} + +# ======================= Main Program ================================ + +# ----------------- Global Vars ----------------- + +# ----------------- Main program ----------------- +print "ch-2.pl (Version 1.0) PWC #33 Task #2: "; + +my $command = shift @ARGV; # Read command + +# -- Execution of simple commands ----------------- +if(! $command) { # if no command print usage + print "Usage\n"; + usage(); +} +elsif($command eq "html") { # if command html or help + print "HTML\n"; + html(); +} +elsif($command eq "help") { + print "Help\n"; + usage(); +} +elsif($command eq "task2") { + print "Task #2\n"; + my $opts = options(); + if($opts->{array}) { + my $aref = task2_create_array(); + output_array($aref); + } + elsif($opts->{simple}) { + task2_simple(); + } + elsif($opts->{pdl}) { + task2_pdl(); + } + elsif($opts->{slice}) { + task2_slice(); + } + else { task2_pdl(); } +} + +# -- Options for "simple" command + +sub options { + my %opts = ( array => 0, simple => 0, pdl => 0, slice => 0 ); + my $o = GetOptions ( # Read options + "array" => \$opts{array}, + "simple" => \$opts{simple}, + "pdl" => \$opts{pdl}, + "slice" => \$opts{slice}, + ); + if(!$o) { usage(); die("Error in command line arguments.\n"); } + return \%opts; +} + +# ================================ Usage ============================ +sub usage { + print "./ch-2.pl [<command>] [<options>]\n"; + print "\n"; + print " command, help|html|task2 \n"; + print " help, Prints out some usage information.\n"; + print " html, Writes HTML and CSS from POD.\n"; + print " task2, Solution for Task #2 (Default PDL). \n"; + print " options:\n"; + print " --pdl, Task #2 PDL solution.\n"; + print " --array, Task #2 Array solution.\n"; + print " --simple, Task #2 Simple solution.\n"; + print " --slice, Task #2 Slice solution.\n"; + print "\n"; + print " Examples:\n"; + print " # perldoc ch-2.pl\n"; + print " # ./ch-2.pl help\n"; + print " # ./ch-2.pl html\n"; + print " # ./ch-2.pl task2\n"; + print " # ./ch-2.pl task2 -p\n"; + print " # ./ch-2.pl task2 --pdl\n"; + print " # ./ch-2.pl task2 --array\n"; + print " # ./ch-2.pl task2 --simple\n"; + print " # ./ch-2.pl task2 --slice\n"; +} + +sub html { + # ------------- Create HTML -------------- + qx[ pod2html --header --title \"Perl Weekly Challenge #33 Task #2, Multiplication Table\" --css \"pwc.css\" ch-2.pl > ch-2.html ]; + + # ------------- Create CSS -------------- + my $CSS =<<CSS; +body { margin-left:auto; margin-right:auto; width:80%; } +h1 { border-bottom:4px solid red; } +h2 { border-bottom:2px solid orange; } +pre { border:2px solid grey; background-color:#eef; padding:10px; } +li { padding:5px; } +a { text-decoration:none; color:black; padding:4px; } +a:hover { background-color: brown; color:white; } +._podblock_ { width:100%; background-color:black; color:white; padding:10px; } +CSS + + open(CSS, ">pwc.css") or die "Cant open pwc.css!\n"; + print CSS $CSS; + close CSS; +} + +=head1 AUTHOR + +Chuck + +=cut + +# ############################## END ############################################# + diff --git a/challenge-033/burkhard-nickels/perl5/example.txt b/challenge-033/burkhard-nickels/perl5/example.txt new file mode 100644 index 0000000000..2fe6575e76 --- /dev/null +++ b/challenge-033/burkhard-nickels/perl5/example.txt @@ -0,0 +1 @@ +The quick brown fox jumps over the lazy dog. |
