Re: seeking advice on loops
by jZed (Prior) on Oct 07, 2005 at 04:36 UTC
|
As you'll probably find out, asking for help with homework isn't looked on too favorably here, but you've certainly been honest about it and it also sounds like you've put some work into it. Perhaps these two questions will help you find an answer: 1) is getting user input something you want to do once or many times? 2) is the loop something that occurs a set number of times, or should it keep occurring until a given condition changes? If you can answer those questions, I think you'll answer your other questions as well. | [reply] |
|
|
As you'll probably find out, asking for help with homework isn't looked on too favorably here
I think that what is frowned upon not only here, but probably also in any (online) Perl community is not "asking for help with homework" as much as one's asking people to do homework for him/her, especially without explicitly saying so. But sierrastar has asked for advice wrt homework, clearly stating so. In this case I think it won't be any problem giving that help...
| [reply] |
|
|
thank you for your input...I know you probably get asked for homework help all the time. I do want to do it myself otherwise I won't be able to go on with the rest of the book. I just feel like this program was a big jump from the chapter 6 homework
in answer to your questions:
2) is the loop something that occurs a set number of times, or should it keep occurring until a given condition changes?
the loop of user input keeps occuring - thru every loop (enter product name, enter quantity and price) the user is asked if they want to enter another product - when they answer "no" is when you leave the loop and invoke a subroutine to process the data
Thanks so much monks...I do appreciate your time
sierra
| [reply] |
|
|
| [reply] |
|
|
user_wants_to_continue is 'yes'
while ( user_wants_to_continue is not 'no' ) {
prompt for and get product_name
prompt for and get quantity
prompt for and get price
store product_name in array
store product_name, quantity, price in hash
prompt for and get user_wants_to_continue
}
| [reply] |
Re: seeking advice on loops
by blazar (Canon) on Oct 07, 2005 at 08:55 UTC
|
we are suppossed to enter a loop, and prompt the user for a product name and ad it to an array and then prompt the user for the quantity and then store the value in a hash (both of these are accessed later to print on a receipt.)
I have two questions:
1. is the user prompted embedded in the loop our outside of it? The book gives examples of loops dealing with arrays that are hard-coded and not input by users (or at least I cant find one)
From the description above I would say inside the loop.
From your last comment I guess you have familiarized with loops iterating over arrays elements. Of course that's not the only way to deal with arrays (in loops or wherever else). For example:
my @array;
while (my $new=whatever You::Like) {
push @array, $new if is($new)->good;
}
2. and I am not sure if I would use a "foreach" or a "while"
A priori both would do. It all depends on how you decide how to do it, TMTOWTDI applies!! However one of them is "more natural" in this situation than the other. You should have enough clues by now know which is which...
PS: to highlight Perl keywords in a visually distinctive manner instead of quoting you can use inline <c> or </code> tags. | [reply] [d/l] [select] |
Re: seeking advice on loops
by Moron (Curate) on Oct 07, 2005 at 09:41 UTC
|
| [reply] [d/l] |
|
|
I appreciate original posters' honesty.
He made clear that it's for a homework exercise, and he made clear that he was looking for advice. Many others have offered advice without offering code.
I beleive you are wrong offering code. The OP probably wishes to try to write it himself (code-wise and not analysis-wise).
Obviously, your code is good and I beleive that you wrote it with good intentions, and would be extremely helpful after the OP has delivered his homework, but at this time I think you should remove it, as a favor to the original poster (if he hasn't read that already).
| [reply] |
|
|
Fair point - I have now put readmore tags around it and a warning.
| [reply] |
|
|
Re: seeking advice on loops
by cowboy (Friar) on Oct 07, 2005 at 17:38 UTC
|
The first thing you should do, is forget code, and think about the logic.
- 1. Enter a loop (I guess you'll be inputting more than 1 product by this statement)
- 2. Ask for the product name. Once it's given, store it, as you were instructed to.
- 3. Ask for a quantity. Once it's given, store it how you were instructed to, (probably the hash linking the name, to the quantity, not sure what use the array is personally).
- Return to 1 for another product.
Once you have the logic figured out, the code is trivial.
| [reply] |
|
|
Perl monks...I have been working on this for awhile now on my own... I am totally new to Perl and programming so I really appreciate your time
I put some print tags in the code to see if I was adding to each hash correctly, however, it seems to only hold the current key-value pair. I realize this code is not streamlined or as efficient as the monks would code it - but I am just learning - once again thanks for your advice
So two questions: why are my keys not assigning the actual value of $itemname?
why isnt the hash(es) keeping all the key-value pairs? I have to have a separate hash for quantity and price (that is part of the assignment)
of course I will continue to seek answers on my own
#!/usr/bin/perl -w
#cash register program
#retreive date and time
########set up hashes##########
##############User input here#######
print "Please enter your first and last name\n";
chomp (my $name = <STDIN>);
print "Please enter your state sales tax in percentage\n";
chomp (my $tax = <STDIN>);
####enter loop here#######
$continue='Y';
while ($continue eq 'Y'){
print "Please enter an item or product name\n";
chomp ($itemname = <STDIN>);
push @item, $itemname;
print "@item\n";
print "Please enter the quantity of the item\n";
chomp ($itemquantity = <STDIN>);
my %qty = ("$itemname" => $itemquantity);
@qty=keys(%qty);
print "@qty\n";
print "Please enter the price of the item\n";
chomp ($itemprice = <STDIN>);
my %price = ("$itemname" => $itemprice);
@price=keys(%price);
print "@price\n";
print "Would you like to add more item(s)? - Y or N\n";
chomp ($continue = <STDIN>);
}
thanks - sierra
| [reply] [d/l] |
|
|
I assume this is still the same homework assignment - I see some progress here. Going through it... there is nothing in the "set up hashes" section. That seems odd. We'll get back to that. And you're missing "use strict". You're using "-w" instead of "use warnings" which is ok, but not advised. Use "use warnings" instead, unless your teacher/professor is telling you otherwise (in that case, pass the course, but switch to this style afterwards).
In the user input section, you have some interesting indentation. I find the extra space in front of the chomps to be distracting and would encourage you to discard them. However, that said, whitespace is a good thing - you could put a blank line between the first print/chomp and the second:
print "Please enter your first and last name\n";
chomp (my $name = <STDIN>);
print "Please enter your state sales tax in percentage\n";
chomp (my $tax = <STDIN>);
The loop section starts with an undeclared $continue variable. Using strict would have caught this. You probably mean to have a "my " in front of this.
Inside the loop, we have more of this inconsistant indentation scheme. Indentation doesn't mean a thing to perl. It means a lot to the human reader who is unfamiliar with your work - which may be you 6 months from now, but, of a more immediate nature, will be your teacher/professor in a few days. Easy to read == easy to mark ;-)
Inside here we see that you create a new %qty hash and a new %price hash (using "my"). You probably intended that the "my %qty" and "my %hash" declarations to go into the hash initialisation section above. (I told you we'd get back to it.) Also, when you assign to an entire hash, such as when you say %qty = ( $itemname => $itemquantity );, you're removing anything that was already there and inserting the new hash (list) in its place. In this case, there's nothing there because you're creating a new hash each time, but we just talked about fixing that, so you would encounter this secondary problem. The syntax you're looking for is $qty{$itemname} = $itemquantity;. This will single out a single element of the hash, and set its value, regardless of whether it was there previously or not.
Of really minor importance at this point is the final question - "Would you like to add more item(s)?" First, you may really mean "Would you like ot add another item?" I just like getting rid of "(s)"s any time I see them. And you don't check that the value typed in is what you want to allow. If I type in "y", you're not going to continue, for example. Or if I type in "Yes, I really do." Or "wallace and grommit". But that's probably not a big deal at this stage of your course - you'll be able to address that in the future once you get the rest of the assignment finished ;-)
Good luck! | [reply] [d/l] [select] |
|
|
|
|
|
|
|
Re: seeking advice on loops
by EvanCarroll (Chaplain) on Oct 07, 2005 at 04:56 UTC
|
| [reply] |
|
|
| [reply] |
|
|
You could look at the poll results of
"If I was forced to use only one kind of loop for the rest of my days it would be a ...."
Perhaps a link would help, since the person you're responding to is a PM newbie.
It could help you better in what monks feel the most powerful loop.
If this is supposed to be sarcastic, then fine. If it's not, then I doubt it would really help. In particular the most voted option has been (as of this writing): "matter of time 'til I killed the bastard who forced me to do it".
Indeed the joke does stress that TMTOWTDI rules. More seriously, the two most often used loops in Perl are IMO while and for. Matter is they excel in two different kind of loops. Typically, but not exclusively of course, the former for iterating over filehandles (such a typical operation in Perl!) and the latter over fixed length lists -- but beware of redo, last and next!
Incidentally we may note here that in Perl6 we will have for commonly used for both kind of situations, with the help of lazy lists and similar magic...
prad
PERL is the only language i know and i dont want to know the rest.
Is this supposed to be a .sig? Then you should do us all a favour and render it in a visually distinctive manner, e.g. a smaller font, to help us not be distracted by it and consider it part of your actual post.
BTW: quite sad that such a claim is about "PERL", especially since there's not such a thing. Please check
perldoc -q 'difference between "perl" and "Perl"'
(also available here.) | [reply] [d/l] [select] |
Re: seeking advice on loops
by nothingmuch (Priest) on Oct 09, 2005 at 12:27 UTC
|
Hi,
I hope you can answer these simpler questions, and then translate them into your original questions.
Outside/inside (or what is repeated): How many times is the user prompted? Answer that question to yourself with either one or zero to infinity.
Remember that a loop is a way to get something you wrote once to happen many times. If you write the code that reads from the user once, and then want it to actually happen many times, you must enclose that code in a loop. Then you must instruct the loop how to repeat the inner part.
Foreach / while (or how it is repeated): Do you know how many items you will have in advance? A foreach loop takes an array that is already filled up and then runs the enclosed part once for each element.
A while loop reiterates as long as a condition is true. Is the user finished inputing data? asks the question "are we done?". This translates to while the user hasn't finished entering data, or more simply while the user still wants to enter data. Compare this with for each piece of data the user entered.
I hope this makes things clearer =)
| [reply] |