Big Data‎ > ‎BigData - Data‎ > ‎

BigData-Stats

<< back to BigData

Displaying the Statistics

Now we need to show the popularity and the total number of names in this list

 We did "find" on "Sam" and we got this:
                                   
                       



    We see that the name "Samuel" was popular with 497 people given that name. 

    But how popular was it? Where does it Rank? What number was it?


A. Finding the Popularity (Rank)

We can find that out by seeing what line number it is now in the list. When LiveCode finds it, it reports what line it was found on. That function is called "foundLine" and we can try it out in the Message Box:

                  

        
We can see that it was # 972  of the most popular names. 

If we want to put that number on the screen, 
    1) create a field "rank" on the card
    2) put word/item #2 of that "foundLine()" result.

To pick just that number, we need to:
    
    set the itemDelimiter to space
    put item 2 of the foundLine() into field "rank"

or more simply

    put word 2 of the  foundLine into field "rank"


B. Finding the Total # of unique names and the Total # of people in the list

Add 2 fields, call them "totalNames" and "TotalPeople"

1. The number of unique names is the number of lines in the file. (There is a line for each name). That can be found by coding:

        
put the number of lines of field "names" into field "totalNames"

2. The total number of people in this list can be gotten by adding up the number of people for each name

      put 0 into count
      repeat for each line x in field "names"                             
            add item 3 of x to count                                        
      end repeat
      put count into field "totalPeople"

We will fill in the numbers when we load the file. 
  






Another Variation of the Program - This separated it into boys and girls
          
                              

D. The Final Code:
      A. On "Load Files" button

on mouseUp
   global totalNames, bCount, gCount

   answer file "Select a Txt file"                                        // get the name of the file
   if it is empty then  exit mouseUp
   put it into fileName

   put url ("file:" & fileName) into theNames         // get the names from the file into the variable "theNames"
   put the number of lines of theNames into totalNames  // put the number of names to the screen

   sort theNames   

   put 0 into totalNames
   put empty into field "girls"
   put empty into field "boys"

   repeat for each line x in theNames                                //separate into "boys" and "girls"
      if item 2 of x = "M" then
         put item 1 of& " - " & item 3 of x  & return after b1   // item 1 = the name, item 3 = the count/number
      else
         put item 1 of& " - "  & item 3 of x  & return after g1
      end if
      add item 3 of x to totalNames                                        // add to the total of all counts
   end repeat

   put totalNames into field "n2"                                        // put it on the screen

   sort lines of b1 descending numeric by item 3 of each 
   put b1 into field "boys"

   sort lines of g1 descending numeric by item 3 of each
   put g1 into field "girls"

end mouseUp


       B. On "Find Name" button

on mouseUp
   put field "myname" into x
   find in field "boys"
   
set itemDelimiter to space
   put item 2 of  foundline() into tNum
end
 mouseUp

Comments