Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^3: help with user selected hash operations?

by poj (Abbot)
on Oct 30, 2017 at 18:55 UTC ( [id://1202360]=note: print w/replies, xml ) Need Help??


in reply to Re^2: help with user selected hash operations?
in thread help with user selected hash operations?

If you indent this block

if ($choice eq 'a'){ print "Enter a male name: "; chomp (my $name1 = ucfirst lc <STDIN>); } if (exists $son_father{$name1}) { print "Duplicate name -- try again!\n"; } else { print "Add a father: "; chomp (my $add_dad = ucfirst lc <STDIN>); $son_father{$name1} = {$add_dad}; next; }

correctly you can see it is 2 blocks not 1

if ($choice eq 'a'){ print "Enter a male name: "; chomp (my $name1 = ucfirst lc <STDIN>); } if (exists $son_father{$name1}) { print "Duplicate name -- try again!\n"; } else { print "Add a father: "; chomp (my $add_dad = ucfirst lc <STDIN>); $son_father{$name1} = {$add_dad}; next; }

What you want is probably

if ($choice eq 'a'){ print "Enter a male name: "; chomp (my $name = ucfirst lc <STDIN>); if (exists $son_father{$name}) { print "Duplicate name $name -- try again!\n"; } else { print "Add a father: "; chomp (my $add_dad = ucfirst lc <STDIN>); $son_father{$name} = $add_dad; print "$name added with father $add_dad\n"; } next; }
poj

Replies are listed 'Best First'.
Re^4: help with user selected hash operations?
by lunette (Acolyte) on Oct 30, 2017 at 19:05 UTC

    i was told this last night by a friend on twitter! but for some reason when i tried to write this, the code insisted it wasn't correct. it works when you write it the way you've done it. i wonder why it told me i was wrong. thank you!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1202360]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-18 18:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found