in reply to A Beginner Needs Homework help
Great to see you using strictures. 'use warnings' is getting you the "Odd number of elements in hash assignment" message and that is telling you important stuff, even though it doesn't mean much to you yet. But before we get to that:
Remove the ; from the #! line. I suspect on a *nix box that will cause you grief, although the whole line is mostly ignored by Windows.
Use three parameter open and a lexical file handle:
open my $fIn, '<', $ARGV[0] or ...
You only make one assignment to $info using <FH> so you only ever read the first line from the file. Maybe there should be a while loop somewhere around there?
The my $fIn helps strict help you. The '<' makes it clear that you want an input file handle and avoids some potential traps with the two argument version of open.
Your while loop doesn't buy you much, especially as you create a new variable inside the loop which gets the value undef. It's fortunate (in a way) that the match controlling the loop fails for your data because otherwise you'd get a stream of using uninitialised variable warnings until you stopped the script running. What is the intent of that loop?
I wouldn't call an array that gets a word and a number 'nums' - that just seems plain wrong. 'values' would be a better name. An even better technique is:
my ($colour, $value) = split /:/, $info;
The my () makes a list so you can assign the list generated by split to the list of variables created by my.
Finally, the warning is because $word = $sum is an assignment so ($word = $sum) is a list containing the value of $sum which is probably not what you intended. However, you need to rethink the entire structure of your program. You need to loop over all the lines of the file and update colour counters as you go. A hash is the right structure for the update process so you need to keep thinking that way. Think about how you would do this if you had pieces of paper (the values) in pigeon holes (the hash) and were getting the data a line at a time over the phone.
Have another crack at this and post the result for further comment.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: A Beginner Needs Homework help
by Swizzlestix617 (Novice) on Apr 10, 2014 at 04:38 UTC | |
by kcott (Archbishop) on Apr 10, 2014 at 06:41 UTC | |
by GrandFather (Saint) on Apr 10, 2014 at 04:48 UTC | |
by MidLifeXis (Monsignor) on Apr 10, 2014 at 13:21 UTC |