# Programming Puzzle 7 - Yet another nested loop!

• 06-09-2011, 04:34 PM
Tom Cone Jr
Programming Puzzle 7 - Yet another nested loop!
Beginning Xbasic Programming

Puzzle 7 - Yet Another Nested loop

You solved Puzzle_6, but your sales manager tells you that she forgot something
that she needs. She doesn't really care how many separate invoices an item
might have been on, she wants to know the amount of money each product generated.

Just like a manager, right?

Your job now is to write an xbasic script that will list each product
and for each product show the total dollars generated by that item
across all the invoices.

Instructions:

a) you may not use any of the built in summary functions, methods,
or operations in Alpha Five. We're doing this one by hand.

b) you may open the table only once

c) your output should be directed to the trace window

d) your list should be in ascending order based on the Product_id field
• 06-09-2011, 05:07 PM
Stan Mathews
Re: Programming Puzzle 7 - Yet another nested loop!
As before.

Attachment 28442
• 06-09-2011, 05:39 PM
aschone
Re: Programming Puzzle 7 - Yet another nested loop!

Code:

```'Date Created: 09-Jun-2011 11:29:00 AM 'Last Updated: 09-Jun-2011 04:16:52 PM 'Created By  : andy 'Updated By  : andy OPTION STRICT OPTION ENCRYPTED_TOKENS ON ERROR GOTO ERR_HANDLER DIM vInvoice_ItemsP as P DIM vOrderC as C DIM vFilterC as C DIM vFormatC as C DIM vIndxN as N DIM arrInvoice[0] as P DIM arrInvoice[0].ProductC as C DIM arrInvoice[0].ExtensionN as N vInvoice_ItemsP = table.open("invoice_items",FILE_RW_SHARED)         vOrderC = "Product_id"         vInvoice_ItemsP.order(vOrderC)         vInvoice_ItemsP.fetch_first()         WHILE .not. vInvoice_ItemsP.fetch_eof()                 vIndxN = arrInvoice.find(vInvoice_ItemsP.Product_Id, "ProductC")                 IF vIndxN > 0 THEN                         'Product found                         arrInvoice[vIndxN].ExtensionN = arrInvoice[vIndxN].ExtensionN + vInvoice_ItemsP.extension                 ELSE                                'Product not Found                         arrInvoice[].ProductC = vInvoice_ItemsP.product_id                         arrInvoice[..].ExtensionN = vInvoice_ItemsP.extension                 END IF                 vInvoice_ItemsP.fetch_next()         END WHILE vInvoice_ItemsP.close() vFormatC = "ProductC|ExtensionN" trace.WriteLn(arrInvoice.dump_properties(vFormatC)) END '----------------------------------- 'Error handler '----------------------------------- ERR_HANDLER:         Dim err as N= error_code_get()         Dim msg as C= "Error # "+ err + crlf() + error_text_get(err)         msg = msg + crlf() + "Script: " + error_script_get()         msg = msg + crlf() + "Line #: " + error_line_number_get()         trace.writeln(msg)         ui_msg_box("Error", msg, ui_attention_symbol)         msg = ""                 END```
• 06-10-2011, 06:56 AM
Tom Cone Jr
Re: Programming Puzzle 7 - Yet another nested loop!
Here's my solution.

The attachment is the "exported" script. In this format the script can be easily "imported" into your copy of the puzzle database.

-- tom
• 08-08-2011, 05:41 PM
jeb richardson
Re: Programming Puzzle 7 - Yet another nested loop!
puzzle attempt for puz 7!

Attachment 29015