in reply to help with user selected hash operations?
Some thoughts:
If you're learning Perl, always use strict; and use warnings;c:\@Work\Perl\monks>perl -wMstrict -le "use strict; use warnings; ;; use Data::Dump qw(dd); ;; my %son_father; ;; my $name1 = 'Jeff'; my $add_dad = 'Doug'; ;; $son_father{$name1} = {$add_dad}; ;; dd \%son_father; " Odd number of elements in anonymous hash at -e line 1. { Jeff => { Doug => undef } }
Is it more clear that after the $choice eq 'a' if-test is done, the exists $son_father{$name1} if-test will always be done and, at least the first time through the loop, will always be false, so the else-clause print "Add a father: "; will always execute? In a similar way, work through the other conditional statements in the loop body.while (1) { # display the menu print $menu, "\n\n"; # get the user choice print "Make your choice: "; chomp($choice = lc <STDIN>); # fulfill the user request if ($choice eq 'a'){ print "Enter a male name: "; chomp ($name1 = lc <STDIN>); } if (exists $son_father{$name1}) { print "Duplicate name -- try again!\n"; } else { print "Add a father: "; chomp ($add_dad = lc <STDIN>); $son_father{$name1} = {$add_dad}; } ... } # end while loop
Give a man a fish: <%-{-{-{-<
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: help with user selected hash operations?
by lunette (Acolyte) on Oct 30, 2017 at 07:51 UTC |