Pathologically Eclectic Rubbish Lister | |
PerlMonks |
Re: help with user selected hash operations?by kcott (Archbishop) |
on Oct 30, 2017 at 11:48 UTC ( [id://1202325]=note: print w/replies, xml ) | Need Help?? |
G'day lunette, Welcome to the Monastery. "i am at the end of my rope here, honestly ..." Don't Panic! There are no insurmountable problems here. "it feels like nothing is working. ..." Most of the individual parts of your code are fine; in fact, for a raw novice, it's pretty good (I've certainly seen a lot worse). I get the impression that you've got the basic logic correct in your head — you just haven't translated that to Perl code. In the following, I've aimed to highlight the good and the bad, and how to fix the latter. Your assignment may have read something like "Do A, B and C only using X, Y and Z", but you haven't told us that. You also haven't said what the main focus of the assignment was: are you learning about hashes? I/O? UI presentation? something else? If you have another question at a later date, that's useful information to include. I've tried to keep my suggestions in line with with the level of code you've presented; for instance, I haven't used any references or regular expressions. If I've used something you weren't supposed to use, and you can't determine an alternative, just ask. The first thing I had a problem with was your indentation. Your first if block is like this:
At first glance, it appears that '}' is the closing brace for the if block. But it's not! That's the closing brace for a different if block: one that appears in "...", indented two more levels, inside another line of code. Indentation doesn't matter to Perl (it does in some other languages and file formats); it does, however, matter a lot to humans who have to read, and possibly modify, the code. You've done the same thing with "if ($choice eq 'd') ...". In both cases, Perl sees a code structure like this:
However, what you really wanted was a code structure more like this:
Fixing that code structure throughout, will resolve a lot of your current issues. Also, instead of a series of if statements, all of whose conditions Perl has to check; consider the following construct, where conditions are only checked until one is found to be TRUE.
There may be better ways to handle that but, as I said at the start, I'm aiming to keep things simple. See "perlintro: Conditional and looping constructs" for more about that. [By the way, the expression if loop is wrong. If you call it a loop, and start thinking in terms of loops, you likely add another level of confusion.] Another problem you have is with changing cases. It's good that you thought to canonicalise the input; unfortunately, you haven't got it quite right. If the user enters "Jeff", "JEFF", or any other combination (perhaps even something daft like "jEfF"), lc will change that to "jeff":
But, "jeff" isn't one of the keys of '%son_father' — "Jeff" is though (oops!). You can fix that by also using ucfirst:
Although there would be better ways to do that in general code, it works with the data you've presented, so I've avoided anything more complex. You can use it for both the keys and values. You do need both the ucfirst and the lc:
If you deal with those two main areas, you can probably get your program up and running reasonably well. There are a few other areas which, while not actually breaking your current code, are poor choices, bad practices, and habits you don't really want to get into.
Here's an example script that just implements the "a" and "e" menu selections. It has examples of all the points I've made above.
Here's a sample run:
— Ken
In Section
Seekers of Perl Wisdom
|
|