Page 199 -
P. 199

function to hash


           Return a data structure from a function


           Processing one line of surfer data was pretty straightforward. But now you have
           to work with all the lines of data in the file. Your program has to make the data
           available quickly so that a request to display the details of a particular surfer can
           be performed as soon as possible.
          You already know enough to write a function that takes the surfer ID as a
           parameter, searches the file one line at a time for a matching ID, and then returns
           the found data to the caller:


                                                   Pass the surfer ID.




                       ...
               find_details(id)
                                                                                       find_     Search the file
                       ...                                                              details()  for the ID



                                                                         Return all the

                                    101;Johnny 'wave-boy'                data for the ID.
                                    Jones;USA;8.32;Fish;21




           There are really only two choices for how you return data from this function. Pass
           back the surfer’s data either:

                     As a string

           or
                     As a hash


           But which? Returning a string requires the calling code to further process the data to
           extract the information it needs, which (although possible) gets messy, because the calling
           code is then required to cut up the string using split(). This is something best left to
           the function, because it hides the complexity of manipulating the data from the calling code.
           Returning a hash allows the calling code to simply pick out the information it needs
           without too much fuss and without any further processing.


           Return a hash from the function to keep the calling code simple.

           164    Chapter 5
   194   195   196   197   198   199   200   201   202   203   204