diff options
| author | Noud Aldenhoven <noud.aldenhoven@gmail.com> | 2019-09-25 10:46:24 +0200 |
|---|---|---|
| committer | Noud Aldenhoven <noud.aldenhoven@gmail.com> | 2019-09-25 10:46:24 +0200 |
| commit | c74c50b388288882019291f29434f96f2e3413ee (patch) | |
| tree | 0031f3763eaa9c5ea1a2236ae18971f35bf0a327 | |
| parent | 8d5490a332ed12af6a6a338392a787c0bdd63661 (diff) | |
| download | perlweeklychallenge-club-c74c50b388288882019291f29434f96f2e3413ee.tar.gz perlweeklychallenge-club-c74c50b388288882019291f29434f96f2e3413ee.tar.bz2 perlweeklychallenge-club-c74c50b388288882019291f29434f96f2e3413ee.zip | |
Several updates, documentation, and fixes for challenge 2
For example, I can run exercise one through this script:
> perl6 ch-2.p6 ch-1.p6
(0 0)
(0 0)
(0 0)
Expected error, because the input doesn't represent a line.
Variables history:
disc = (8, 0)
| -rw-r--r-- | challenge-027/noud/perl6/ch-2.p6 | 67 |
1 files changed, 43 insertions, 24 deletions
diff --git a/challenge-027/noud/perl6/ch-2.p6 b/challenge-027/noud/perl6/ch-2.p6 index 0d7e6d9f8f..9b6b316e31 100644 --- a/challenge-027/noud/perl6/ch-2.p6 +++ b/challenge-027/noud/perl6/ch-2.p6 @@ -5,37 +5,56 @@ # # After the above operations, it should list $x historical value in order. +# The idea behind this solution is to first extract all defined variables +# from the original script. After that we create a hash %var_hash_ that +# updates the current values of each of the defined variables after each +# semicolon. The new script is executed using the dangerous EVAL method. +# +# Usage: +# perl6 ch-2.p6 filename-of-script.p6 + # I hope I don't get banned from the Perl Weekly Challenge club for using EVAL # in this problem. ;) use MONKEY-SEE-NO-EVAL; -my @variables = (); -my $pattern = /my\s*\$(\w+)/; - -my $exec_prog = ""; -for 'exm.p6'.IO.slurp.split(";") -> $line { - my @line_var = ($line ~~ $pattern).values; - if (@line_var.elems > 0) { - @variables = (|(@line_var), |(@variables)); +sub MAIN(Str $filename) { + # Collect all variables in program. + my @variables = (); + for $filename.IO.slurp.split(";") -> $line { + my @line_var = ($line ~~ /my\s*\$(\w+)/).values; + if (@line_var.elems > 0) { + @variables = (|(@line_var), |(@variables)); + } } - $exec_prog = "$exec_prog $line\;"; - # After every line update %var_hash_ with the current variable values. - for @variables -> $x { - $exec_prog = "$exec_prog \%var_hash_\.push: ($x => \$$x)\;"; + + my $exec_prog = ""; + for $filename.IO.slurp.split(";") -> $line { + $exec_prog = "$exec_prog $line\;"; + # After every line update %var_hash_ with the current variable values. + for @variables -> $x { + $exec_prog = "$exec_prog + if (not DYNAMIC::<\$$x> === Nil) \{ + \%var_hash_\.push: ($x => DYNAMIC::<\$$x>)\; \}\;"; + } } -} -my %var_hash_; -EVAL $exec_prog; # https://xkcd.com/292/ -for %var_hash_.kv -> $var, @hist { - print("$var = ("); - my $last = @hist[0]; - print("$last"); - for @hist[1..*] -> $next { - if ($last != $next) { - print(", $next"); - $last = $next; + my %var_hash_; + EVAL $exec_prog; # https://xkcd.com/292/ + + say "Variables history:"; + for %var_hash_.kv -> $var, @hist { + my @hist_ = @hist.grep({ not $_.^name === "Any" }); + if (@hist_.elems > 0) { + print("$var = ("); + my $last = @hist_[0]; + print("$last"); + for @hist_[1..*] -> $next { + if ($last != $next) { + print(", $next"); + $last = $next; + } + } + print(")\n"); } } - print(")\n"); } |
