Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: Hash Uninitialized Values Error

by GrandFather (Sage)
on Dec 26, 2013 at 04:19 UTC ( #1068392=note: print w/replies, xml ) Need Help??

in reply to Hash Uninitialized Values Error

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

Replies are listed 'Best First'.
Re^2: Hash Uninitialized Values Error
by slinky773 (Sexton) on Dec 26, 2013 at 05:15 UTC
    Thanks so much, man. Someday I'm going to go to my school's "lab" (it's just a library but I always go there to code) and sit down and really fix my bad habits, using this as a first exercise.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1068392]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2017-07-21 19:03 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (334 votes). Check out past polls.