Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Ok, I looked at the code up, down and sideways, then started refactoring common code and realised that half the logic vanishes if the two sets of statements are stored in one structure. I also noticed a bunch of make work creating a transient numbered set of data so I eliminated that. Then the code seemed to simplify down to the point where I could see what was going on. Here's the result:

use strict; use warnings; if (!@ARGV) { print <<HELP; Hi. This is the Lee-Hardy Conclusion Calculator. Given a list of statements on the command line using the syntax "p + -> q" I will deduce the correct conclusion to be made using basic logic an +d the chain rule. - may be used for negation q's can not be duplicated HELP exit; } LHCC(parse(@ARGV)); sub LHCC { my (%all) = @_; my @Conclusion; my @alternate; foreach my $testKey (keys %all) { my @candidate = ($testKey); my $nextKey = $testKey; while ($nextKey) { $nextKey = $all{$nextKey}; push @candidate, $nextKey; last if ! exists $all{$nextKey}; } if (scalar @candidate > scalar @Conclusion) { @Conclusion = @candidate; } elsif (scalar @candidate == scalar @Conclusion) { @alternate = @candidate; } } print join ' => ', @Conclusion; print "\nor\n"; print join ' => ', @alternate; print <<RESULT; In other words: $Conclusion[0] => $Conclusion[-1] or: $alternate[0] => $alternate[-1] RESULT } sub parse { my %all; for my $statement (@_) { my ($p, $q) = $statement =~ /(\S+)\s*->\s*(\S+)/; $all{$p} = $q; $_ = /^-(.*)/ ? $1 : "-$_" for $p, $q; $all{$q} = $p; } return %all; }

Given "-c->-f" "g->b" "p->f" "c->-b" on the command line it prints:

p => f => c => -b => -g or g => b => -c => -f => -p In other words: p => -g or: g => -p

The code is not robust against bad data, but it does at least generate what seems to be the right result with the sample data.

True laziness is hard work

In reply to Re: Hash Uninitialized Values Error by GrandFather
in thread Hash Uninitialized Values Error by slinky773

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2022-01-26 13:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (69 votes). Check out past polls.

    Notices?