Page 165 -
P. 165

too many variables


          You can’t use a separate variable

           for each line of data


           Programming languages use variables to give you access to data in
           memory. So if you are going to store the data from the results.txt
           file in memory, it makes sense that you’ll need to use lots of variables to
           access all the data, right?
           But how many variables do you need?

           Imagine the file just had three scores in it. You could write a program
           that read each of the lines from the file and stored them in variables
           called first_score, second_score, and third_score:                        Each line in the file gets a
                                                            first_                   variable of its own.
                                                            score
           The data in                                                  second_
           the file                                 8.65                score


                                                                                    third_
                        8.65                                    9.12                score
                        9.12
                        8.45                                                8.45






           But what if there were four scores in the file? Or five? Even worse, what
           if there were 10,000 scores? You’d soon run out of variable names and
           (possibly) memory in your computer, not to mention the wear and tear
           on your fingers.


                                          Well, the As are done
                                          and that‛s the first volume
                                          of all the entries from the
                                          Encyclopedia Galactica stored
                                          in memory. Time to tackle the
                                          Bs, then the Cs, Ds, Es....




           Sometimes, you need to deal with a whole bundle
           of data, all at once. To do that, most languages give
           you the array.


           130    Chapter 4
   160   161   162   163   164   165   166   167   168   169   170