in reply to Re: seeking advice on loops
in thread seeking advice on loops

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

Replies are listed 'Best First'.
Re^3: seeking advice on loops
by Tanktalus (Canon) on Oct 09, 2005 at 01:48 UTC

    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!

      thanks so much for your input...I am almost done with this assignment and got thru the user input section...now I have to run a subroutine to print out the array and hashes...of course I have encountered a problem...

      the assignment states to run a subroutine on the @item array and then go thru the two hashes (%qty and %price) and print out each value associated with the $itemname

      so here is my code and I cannot get it to print out the associated values in the hashes that are associated with the $itemname key

      #!/usr/bin/perl -w #cash register program my %qty=(); my %price=(); #####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 to capture customer information here##### my $continue='Y'; while ($continue eq 'Y'){ print "Please enter an item or product name\n"; chomp ($itemname = <STDIN>); push @item, $itemname; print "Please enter the quantity of the item\n"; chomp ($itemquantity = <STDIN>); $qty{$itemname} = $itemquantity; my @qty = values %qty; print "Please enter the price of the item\n"; chomp ($itemprice = <STDIN>); $price{$itemname} = $itemprice; @price=%price; my @price = values %price; print "Would you like to add another item? - Y or N\n"; chomp ($continue = <STDIN>); } ##### process information with a subroutine here ##### foreach $item (@item) { print "Item: $item\n"; print "@qty\n"; print "@price\n"; }

      I am learning as I go thru this but I know it is a slow process for me - thanks for your advice monks

      sorry I posted twice - I thought I lost the last post - thanks for all your input....the final thing I must do is to print the user input by running a subroutine thru @item and %qty and %price (print item name, quanity, and price)

      I am having trouble figuring out how to run thru one array and two hashes in the same subroutine...

      I tried a foreach statement for each one - but that did not work or seem to make sense. I have been trying to figure this our for hours now...

      <any advice on the best way to grab the $itemname from @items and the value from %qty and %price that is associated with the $itemname (the key)? Here is my code so far...thanks monks.

      #!/usr/bin/perl -w #cash register program my %qty=(); my %price=(); #####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 to capture customer information here##### my $continue='Y'; while ($continue eq 'Y'){ print "Please enter an item or product name\n"; chomp ($itemname = <STDIN>); push @item, $itemname; print "Please enter the quantity of the item\n"; chomp ($itemquantity = <STDIN>); $qty{$itemname} = $itemquantity; ### my @qty = values %qty;### print "Please enter the price of the item\n"; chomp ($itemprice = <STDIN>); $price{$itemname} = $itemprice; ### my @price = values %price;### print "Would you like to add another item? - Y or N\n"; chomp ($continue = <STDIN>); } ##### process information with a subroutine here ##### foreach $item (@item) { print "Item: $item\n"; while ( ($key, $value) = each %qty) { print "Quantity: $value\n"; } }

        I highly suggest fixing that identation scheme. It really does make it harder to read. That said, the whitespace between chunks of code is well-placed.

        Also, I continue to suggest that you have "use strict;" near the top of your program as well.

        Notice in your input loop that the scalars that you're putting into @item are also the hash keys you're using for %qty and %price. This would suggest that you want to use the item names in @item as your keys for output as well. Thus, you're looping on the @item list (which is your list of keys), and you can output from all at the same time.

        Your output loop, as it is, becomes something like:

        foreach my $item (@item) { print "Item: $item\n"; print "Quantity: $qty{$item}\n"; }
        Note also how I put in a "my" there for $item so that it would pass strict. I hope that you can extend this to include the output for your price as well.

        Good luck!