Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

Share this Page URL
Help

Chapter 8. Handlers > Saving and Loading Handlers - Pg. 269

Handlers 2. Click the Event Log tab and run the program. You get the following output: (*1, 3, 4, 5*) (*a, x, b, c, d*) How It Works On...end wrappers were placed around the code to remove an item from a list and also to insert an item into a list to turn them into handlers. At the very end of both handlers, the resulting list is also returned. Otherwise, the code remains unaltered from Chapter 6. Note that these lines from the removeItem handler set L to rest of L -- remove first item set L to items 1 thru -2 of L -- remove last item set L to (items 1 thru (n - 1) of L) & (items (n + 1) thru -1) of L and this one from the insertItem handler set L to (items 1 thru (n - 1) of L) & lItem & (items n thru -1 of L) dictate that the modified list must be returned because, as previously described in detail, these state- ments have the effect of setting L to point to a different list in memory. Therefore, they do not always modify the list passed as the argument in place. Saving and Loading Handlers With your general purpose list handling routines, you can go ahead and use them in any program that needs to work with lists. One way to do that is simply to copy the code into any program that needs access to these handlers. As you see in the following Try It Out, another alternative is to save your collection of handlers in a file in the normal way and then load it, using the load script command, into any program that needs to use it. The advantage of this approach is that you don't have to copy the code into each program. And if you ever find a bug in one of your handlers or want to make improvements, you only have to do it in the original file and not hunt down every file that contains the copied code. Try It Out 1. Saving and Loading the List Handlers Now you see how you can load your handlers into another program. Type the list handlers listSum , listSort , removeItem , and insertItem from previous exam- ples. Just type the handlers, not the code at the end of each program that tested them out. For space considerations, I don't show the code again here, just the structure of the program. -- General purpose list handlers -- Insert the code for the listSum handler here 269