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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.