Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^5: My first cpan module - App::ForKids::LogicalPuzzleGenerator

by LanX (Saint)
on Feb 26, 2018 at 19:22 UTC ( [id://1209993]=note: print w/replies, xml ) Need Help??


in reply to Re^4: My first cpan module - App::ForKids::LogicalPuzzleGenerator
in thread My first cpan module - App::ForKids::LogicalPuzzleGenerator

>
if( /\S+,\S+/ ) # if some cell has a comma, fork (sort of) { push @stack, $` . $_ . $' for split /,/, $&; }

Please correct me, but this looks like branching with pure Perl means.

Instead of recursive calls, you are pushing different alternatives on a stack.

Would be more "fun" if the branching was implemented with regexes... ;-)

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^6: My first cpan module - App::ForKids::LogicalPuzzleGenerator
by tybalt89 (Monsignor) on Feb 27, 2018 at 00:51 UTC

    Here's the simpler problem solved by regex.

    #!/usr/bin/perl use strict; use warnings; # Three friends live here. # Each likes a different fruit and has a different profession. # John doesn't like pears. # The programmer likes cherries. # Patrick is a blacksmith. # Edward isn't a fisherman. # Who likes apples? $_ = <<END; John apples,cherries,pears blacksmith,fisherman,programmer Patrick apples,cherries,pears blacksmith,fisherman,programmer Edward apples,cherries,pears blacksmith,fisherman,programmer END my @answer = /^ (\w+)\ \S*\b(\w+)\b\S*\ \S*\b(\w+)\b\S*\n # first line (\w+)\ \S*\b(\w+)\b(??{$2 eq $5})\S*\ # second line \S*\b(\w+)\b(??{$3 eq $6})\S*\n (\w+)\ # third line \S*\b(\w+)\b(??{$5 eq $8 || $2 eq $8})\S*\ \S*\b(\w+)\b(??{$6 eq $9 || $3 eq $9})\S*\n (??{ $2 eq 'pears' }) # John doesn't like pears (??{ $6 ne 'blacksmith' }) # Patrick is a blacksmith (??{ !grep $_ eq 'cherriesprogrammer', # The programmer likes cherr +ies $2.$3, $4.$5, $8.$9 }) (??{ $9 eq 'fisherman' }) # Edward isn't a fisherman /x or die "failed"; print "$1 $2 $3\n$4 $5 $6\n$7 $8 $9\n\n"; print "@answer[ map $_ - 1, grep $answer[$_] eq 'apples', 0 .. $#answer] likes apples.\n";

    outputs:

    John apples fisherman Patrick pears blacksmith Edward cherries programmer John likes apples.

        Many thanks! Lots of fun!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1209993]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-20 01:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found