The stupid question is the question not asked | |
PerlMonks |
Re^3: A Beginner Needs Homework helpby Anonymous Monk |
on Apr 10, 2014 at 23:12 UTC ( [id://1081890]=note: print w/replies, xml ) | Need Help?? |
On the comments Should be :)
Read this if you want to cut your development time in half! Whatever file is associated is vague description for that variable :) Also the whole line isn't described, so
So do you think the program should stop or keep going if the file can't be opened? Maybe try open second ARGument ? Its all about details and diamond operator <> doesn't usually read files :)
For the real name of diamond operator see perlop and/or free book Modern Perl by chromatic a loose description of how experienced and effective Perl 5 programmers work....You can learn this too. Missing explanation, parts (broken missing parts
Important detail, since s///ubstition only replaces what it m//atches first, you don't need to m//atch the same pattern before you s///ubstitute it -- its the same pattern s/patterntomatch/replacementstring/ Your description of the pattern is close but its not exact See if you can figure out the problem by looking for "newline" in perlintro#Simple matching, perlintro#Simple substitution, perlintro#More complex regular expressions You can also use wxPPIxregexplain.pl/ ppixregexplain.pl / rxrx to get an exact description/explanation of the pattern Thats pretty good description (taken out); You could also say the non-colon parts of $info are cut and stuffed into @nums; But its easier to remember if you describe it as "split"ing
This is pretty good.
Verbing the function name, thats good :) But you named the variable $word Hmm, you have $words in @nums? That should be $num or @words ... giving variables good descriptive names is important ... its easy to get confused perlstyle has some advice on naming variables, as do Re: Perl Best Practices for naming variables, About the use of the plural form for the name of variables, Re^3: What would be the easier way, Re^8: What would be the easier way, Re: Cool way to parse Space Separated Value and CSV files, Re: Perl Best Practices for naming variables, Re: What are the most basic, generic aspects of programming?, Pronounceable syntax or beloved punctuation?, How do you pronounce variable names?, How do I pronounce all this stuff, How do you pronounce "::"?, Perl Naming Conventions
An a empty scalar is undef. In other languages an empty variable has random junk. Your variable isn't empty (or even empty string), its a number ################# Well, this line says $var is a copy of $info ... scalars (strings)
Description is pretty good (minus the stuff already covered about $sum)
So since you don't use my $var = $info for anything you should write foreach my $var ( @nums ){ ... } Actually you should write foreach my $num ( @nums ){ ... } since $num is a variable too :) And it gets confusing again
%hash is like @array , not very descriptive ( the % and @ tell us hash and array _ %hashes go key/value, word/definition ... so $sum is not a key, the key is $word There is no color mentioned anywhere ... are you missing a $color variable? Should $word be $color instead? Like mentioned earlier $word is the first @nums ... so you have to straighten this out On the next step Now that I've evaluated the code myself it is definitely missing things.. I just don't know how to build this kind of stuff. I'm such a novice that I had a hard time imagining how to put the pieces together. Nice, this is important realization. You start by stating your goals (make a list) of what you want the program to do ... draw a circle around each goal Then on a new piece of paper you write the steps that you, as a human, would take to accomplish said task One action per line, very detailed .... its like doing long division or other simple math problems, step by step, very very detailed Pretend you're on the telephone talking to your mother and she is trying to get on the internet but can't find the computer, plug it in, plug in the network, turn on the wireless , login ... or how much flour she needs to bake one slice of pizza Programs are like a really really detailed recipes even an idiot can follow -- the computer is the idiot How many plates/pans/bowls/buckets will you need? How do you go from flour in a paper bag to pizza on a plate? Variables are buckets , just storage. Scalars is your basic bucket. A single shelf full of buckets is arrays. A cabinet of labeled drawers is a hash -- the drawers are buckets, the labels are keys, the drawers contain the values To write a program you have to understand when a bucket is in the room with you, when it has the flour you need .. how to move it to the mixer ... its about moving, the transitions, each of those things is a step
So that should be your next step, write in english what the program is supposed to accomplish (goals), then the steps to accomplish said goals (open book, turn the page, find recipee, gather ingredients ...) .... so that you can recognize the variables you need to accomplish each step ... then time to code it; computer programs are only as smart as you, they can only remember what you tell them to remember It all starts with the plan , like building a house; With experience you can do a lot of planning without writing it all down (because you have a mental model, a mental checklist), but you learn by writing it down in english (or native language) Advice about reading a book :) Re: CSV File Parsing, Re: how to read a particular paragraph from middle of a file line by line and enter each string in the line as an element of an array, On debugging, verify everything, talk to teddybear ... checklists and more, Re: How to teach perl to novice programer
In Section
Seekers of Perl Wisdom
|
|