Datagrid

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
bsouthuk
Posts: 261
Joined: Fri Dec 05, 2008 7:25 pm

Datagrid

Post by bsouthuk » Tue Aug 09, 2011 3:17 pm

Hi All

I am working on generating reports from my datagrid and wondered if someone could help me out with some code.

I want to be able to count how many records there are within my datagrid which is very simple:

Code: Select all


   put the dgNumberOfLines of group "Datagrid" into field "CustomerQTY"

However, I need to be able to count the number of 'unique records' there are witin my datagrid going by my 'Customer Name' column. So there maybe 10 records in my datagrid altogether, however some of the records are duplicates. Can someone provide with the code I would need for this?

Your help would be most appreciated.

Daniel

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Datagrid

Post by sturgis » Tue Aug 09, 2011 10:42 pm

You'll have to modify this for your purposes, but I figure it will probably point you the right direction.
Basically, it pulls your datagrid data into a temporary array, cycles through the rows (tKey is the current row index during the repeat loop)
During the loop it fills a new array, keyed by customer name. If there is no entry for the current key being checked, it creates an entry and puts the row index into the contents. If the key already exists, it adds the current index as an item (comma delineated list) this way you have the same number of keys as you do unique records, and you can track where duplicates are.

Not sure how clear this is, but hopefully will be of some help.

## Edited 6:10 PM to try and make the code clearer.

Code: Select all

on mouseUp
   put the dgdata of group 1 into tArrayA -- copy datagrid contents to array

   repeat for each key tKey in  tArrayA -- cycle through 'rows' of array, tKey is the row index

     -- set tKeyVal to the contents of the customer name column  for the row
      put tArrayA[tKey]["Customer Name"] into tKeyVal 

      --check to see if theres already an array key of our new array (tNewArrayA) that 
      --matches the current customer name and if not..
      if tNewArrayA[tKeyVal] is empty then

        -- create new array keyed by tKeyVal
         put tKey into tNewArrayA[tKeyVal] 

      --if there IS already a value in the tNewArray for the current customer name key then
     else

        -- add another item to the keyed entry to track duplicates
         put comma & tKey after tNewArrayA[tKeyVal] 
      end if
   end repeat
   
   -- this just prints an explanatory message to the msg box with # of unique rows based on customer name
   put "There are " & the number lines in the keys of tNewArrayA && "unique records" & return 
                -- outputs the number of keys created, thereby giving a count without dupes
   

-- this just cycles through our new array and displays the datagrid
--index numbers matching each customer name
repeat for each key tKey in tNewArrayA
      put "The name " & tKey && "appears in row(s): " & tNewArrayA[tKey] & return after msg
   end repeat
end mouseUp
Last edited by sturgis on Wed Aug 10, 2011 1:11 am, edited 1 time in total.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10356
Joined: Wed May 06, 2009 2:28 pm

Re: Datagrid

Post by dunbarx » Wed Aug 10, 2011 1:02 am

Try something like this

Code: Select all

on mouseup
   repeat 10
      put any char of "abcdefgh" & return after temp
   end repeat
   sort temp
   
   repeat with y = the number of lines of temp down to 1
      if line y of temp <> line (y-1) of temp then add 1 to unique
   end repeat
   answer unique
end mouseup
Unique will be the number of records that do not match any other. This is very old fashioned stuff, like me.

Craig Newman

Post Reply