View Full Version : Programming Puzzle 9 - Dump ta dump


Tom Cone Jr
06-11-2011, 10:12 AM
Beginning Xbasic Programming

Puzzle 9 - Dump ta dump

You solved Puzzle 6, 7, & 8 but NOW your sales manager tells you
that the list of product sales doesn't do anyone any good displayed on
screen. She needs the list of sales in a text file she can print or
send to others. After noting that it took you longer to do the
descending sort that she had expected, she says dumping the
list to a disk file should be trivial. After all, you're dumping it
to the trace window already, right?

After contemplating what you might want to "dump" on your manager,
you realize that your job now is to write an xbasic script that will list
each product in the Inivoice_items table and for each product show the
total dollars generated by that item across all the invoices. Your list
should be sorted in descending order based on total dollars generated
by each item. And, finally, the list must be written to disk in a simple
text file.


a) you may not use any of the built in summary functions, methods,
or operations in Alpha Five. We're totaling sales without using
any of the built-in summarization tools Alpha provides.

b) you may open the table only once

c) your output should be written to a disk file named "Puzzle_9_Sorted_List.txt"
in a folder called "Temp" on your "C" drive.

d) export your solution script and attach it to your reply, here

e) attach a copy of the text file your script creates when you submit your
solution here

06-11-2011, 10:48 PM
Comments simply regurgitate what the code already tells you. Hence why I hate comments. For Mr. PickyPicky's sake I have added some comments.

Stan Mathews
06-12-2011, 01:33 PM
All I needed to do was use

file.from_string() with the appropriate parameters instead of the loop I had writing to the trace window.


Tom Cone Jr
06-13-2011, 08:38 AM
Very nice work, guys. You both built a long string containing the complete output text. Then you used a higher level function to write the string to your disk file.

Here's my more primitive approach. Key differences are few: (a) script verifies target directory exists or creates it; (b) script gives user the optioin to end early without overwriting an exisitng file; and (c) the file is created one line at a time.

-- tom

jeb richardson
08-10-2011, 06:00 PM
To Mr. Prof. PickyPicky, towards the end of my script there are comment out lines. These lines are alternative ways I discovered to write my array into a txt format document. If the time is available check out the alternatives and see what you think. (The alternatives were introduced to me by Stan.)



08-10-2011, 07:47 PM
Jeb -

Code looks and even better it works.

I didn't validate that your results where correct so cannot attest to the calculations.

I would suggest that you always dimension your variables prior to using them. Alpha 5 does allow you use a non dimensioned variable, but I find this to be bad habit forming. When you are editing your code you can click the "Code" menu option then choose "Find Un-DIMed Variables" This genie will even help you create the code needed to dimension the variables. I prefer to start all my code with the "OPTION EXPLICIT" command, this will cause alpha5 to stop execution if it finds an un-dimmed variable.

On line 27 you create the output file and open it using the file.create() function. However you still have a bit of processing to do before you utilize the opened file. I personally prefer to open files right before i need to read or write data and then close them immediately.

In this case I would move line 27 down to where you are gonna start writing to the file.

creation = file.create(path,FILE_RW_SHARED)
creation.write_line("Product ID"+space(20)+"Totals")

Same with line 62
item.close(). Since you were done using the table at line 49, I would place my <tbl>.close() call there. No sense leaving a table pointer open if you are not using it.

None of this is ground breaking or show stopping. In the end sometimes if the code works just go with :)

Tom Cone Jr
08-10-2011, 08:08 PM
Jeb, I think Andy means



08-10-2011, 09:26 PM
You are correct.

jeb richardson
08-11-2011, 10:12 AM
Awesome. Thanks guys!