Group Duplicate Entries by date and Display in a Field

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
lordjjl
Posts: 4
Joined: Fri Jan 13, 2012 8:28 am

Group Duplicate Entries by date and Display in a Field

Post by lordjjl » Fri Jan 13, 2012 8:58 am

Hi there,

I've been struggling with this one.
I have a text file that is full of tab delimited entries that looks something like this:
"Date" "Customer Name" "Payment" "Payment Type" "Notes"

I want to read that file in and display display item 1 (the date) in a field. The tricky part is that I want to group duplicate date entries together so that they take up one line and display the number of duplicates with it.

I would like to end up with a field that has entries sorted like this:
Monday, January 10, 2011[1]
Monday, January 11, 2011[3]
Monday, January 12, 2011[8]
Monday, January 13, 2011[1]
Monday, January 14, 2011[10]

Any help on where to begin with grouping the dates together would be much appreciated!

Thanks in advance!
Josh

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: Group Duplicate Entries by date and Display in a Field

Post by bn » Fri Jan 13, 2012 11:53 am

Hi Josh,

first of all welcome to the forum.

I attach a stack that does what you want. It uses an array to count the occurences of dates. This may seem at first counterintuitive but at second thought it is probably the fastest/easiest way in your case.
An array in Livecode consists of a "Key" = the identifier of an element of an array and the data to which the key points.
Now a key can be pretty much anything you want it (look up array in the dictionary and the documentation)
The trick is to make the date in your data a key of an array and at the same time add 1 to the data of the key. If a date comes up a second/third etc time so the data will be increased by 1, giving you the count of occurences.
Then you take the keys of the array = your dates and look up what the data of the key is: your occurences.

The little stack makes your data structure as I understood it and a second button gives you the occurences.

I know this might be a little complex at first but with this technique you can count occurences in huge amounts of data.

Of course there are different ways to count the occurences but to me they are more complicated. You could sort your data by item 1 of your data if you set the itemDelimiter to tab. (see button 1 for an example). Then you could go over the sorted list and add 1 to a counter for each line while the line is the same as the previous line, otherwise reset the counter to 1 and start anew.

Play around with the ideas and the stack and please try to look up what is unfamiliar to you.

howManyOccurences.livecode.zip
(1.83 KiB) Downloaded 207 times
Kind regards

Bernd

lordjjl
Posts: 4
Joined: Fri Jan 13, 2012 8:28 am

Re: Group Duplicate Entries by date and Display in a Field

Post by lordjjl » Sat Jan 14, 2012 5:05 am

Wow that works perfectly!

Thank you so much

-Josh

Post Reply