Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

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

I've lightly refactored your code to move input code out of the main sub, remove dead code and add logic sanity checking using die statements for bad logic or data (I can't tell where the error is, but you may).

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 (%statements) = @_; my $NumberOfStatements = keys (%statements) / 2; my %Contrapositives; for my $StatementNumber (1 .. $NumberOfStatements) { $Contrapositives{"P$StatementNumber"} = $statements{"Q$StatementNumber"}; $Contrapositives{"Q$StatementNumber"} = $statements{"P$StatementNumber"}; } for my $Element (keys %Contrapositives) { if ('-' eq substr $Contrapositives{$Element}, 0, 1) { $Contrapositives{$Element} = substr $Contrapositives{$Elem +ent}, 1; } else { $Contrapositives{$Element} = "-$Contrapositives{$Element}" +; } } for my $Element (1 .. $NumberOfStatements) { $statements{$statements{"P$Element"}} = $statements{"Q$Element +"}; delete $statements{"Q$Element"}; delete $statements{"P$Element"}; } for my $Element (1 .. $NumberOfStatements) { $Contrapositives{$Contrapositives{"P$Element"}} = $Contrapositives{"Q$Element"}; delete $Contrapositives{"Q$Element"}; delete $Contrapositives{"P$Element"}; } my @Conclusion; my @OtherConclusion; foreach my $checkKey (keys %statements) { my @PossibleConclusion = ($checkKey, $statements{$checkKey}); CHECKPOINT: foreach my $key (keys %statements) { if (exists $statements{$checkKey} && $key eq $statements{$ +checkKey}) { die "No statement $key" if ! exists $statements{$key}; push @PossibleConclusion, $statements{$key}; $checkKey = $key; } } foreach my $key (keys %Contrapositives) { if (exists $statements{$checkKey} && $key eq $statements{$ +checkKey}) { die "No statement $key" if ! exists $statements{$key}; push @PossibleConclusion, $statements{$key}; $checkKey = $key; goto CHECKPOINT; } } if (scalar @PossibleConclusion > scalar @Conclusion) { @Conclusion = @PossibleConclusion; } elsif (scalar @PossibleConclusion == scalar @Conclusion) { @OtherConclusion = @PossibleConclusion; } } foreach my $checkKey (keys %Contrapositives) { die "No statement $checkKey" if ! exists $statements{$checkKey +}; my @PossibleConclusion = ($checkKey, $statements{$checkKey}); CHECKPOINT2: foreach my $key (keys %statements) { next if $key ne $Contrapositives{$checkKey}; die "No statement $key" if ! exists $statements{$key}; push @PossibleConclusion, $statements{$key}; $checkKey = $key; } foreach my $key (keys %Contrapositives) { next if $key ne $Contrapositives{$checkKey}; die "No statement $key" if ! exists $statements{$key}; push @PossibleConclusion, $statements{$key}; $checkKey = $key; goto CHECKPOINT2; } if (scalar @PossibleConclusion > scalar @Conclusion) { @Conclusion = @PossibleConclusion; } elsif (scalar @PossibleConclusion == scalar @Conclusion) { @OtherConclusion = @PossibleConclusion; } } print join ' => ', @Conclusion; print "\n\nor \n\n"; print join ' => ', @OtherConclusion; print <<RESULT; In other words, $Conclusion[0] => $Conclusion[(scalar @Conclusion) - 1 +] or $OtherConclusion[0] => $OtherConclusion[(scalar @Conclu +sion) - 1] RESULT } sub parse { my %statements; my $n = 1; for my $statement (@_) { my ($p, $q) = $statement =~ /(\S+)\s*->\s*(\S+)/; $statements{"P$n"} = $p; $statements{"Q$n"} = $q; ++$n; } return %statements; }

Given "-c->-f" "g->b" "p->f" "c->-b" as input the following error is generated:

No statement -b at test.pl line 72.

Points of interest:

  1. the script parses statements entered on the command line to avoid an extended and error prone Q&A session
  2. unreachable and empty statements removed
  3. use of join to generate result lists
  4. use of heredoc (perlop - Quote Like Operators look for <<EOF) for most multiple line print statements

I haven't fixed the processing logic because after a quick look I can't figure out what it's trying to do. However I suspect a recursive routine would be clearer, avoid the gotos and be shorter.

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 romping around the Monastery: (1)
As of 2022-01-26 07:19 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?