Sorting data question

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: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
DavJans
Posts: 240
Joined: Thu Dec 12, 2013 4:21 pm
Location: Spokane, WA USA

Sorting data question

Post by DavJans » Tue Jun 18, 2019 11:55 pm

I am having a hard time figuring out how to sort and comment on data. The data comes in with a variable number of lines.

Simple example data:

Shape,Grade,Thicknes,Dimensions,PieceMark
PL,A36,1/4,1' 9-3/4 x 12' 0-1/2,p2103
PL,A36,1/4,4' 3-3/4 x 5' 6-1/2,p2104
PL,A36,3/16,11' 7-3/4 x 30' 6-1/2,p2105
PL,A36,3/16,1' 8-1/2 x 9' 0-1/2,p2106
PL,A36,1/4,9' 0-7/8 x 4' 0-1/2,p2107

If all of this is in tData, how would I split it up into tData1, tData2, tData(X), etc...
This is a very small amount of data norlmally would be arround 700 lines at a time with many shape, grade thicknesses and we need it separated by all 3.

tData1
Shape,Grade,Thicknes,Dimensions,PieceMark
PL,A36,1/4,1' 9-3/4 x 12' 0-1/2,p2103
PL,A36,1/4,4' 3-3/4 x 5' 6-1/2,p2104
PL,A36,1/4,9' 0-7/8 x 4' 0-1/2,p2107

tData2
Shape,Grade,Thicknes,Dimensions,PieceMark
PL,A36,3/16,11' 7-3/4 x 30' 6-1/2,p2105
PL,A36,3/16,1' 8-1/2 x 9' 0-1/2,p2106
"Det bästa stället att hitta en hjälpande hand är i slutet av din egen arm"

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6716
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Sorting data question

Post by FourthWorld » Wed Jun 19, 2019 1:26 am

Have you tried the sort command? Which part do you want to sort by? And what do you mean by "comment on data"?
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6072
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Sorting data question

Post by dunbarx » Wed Jun 19, 2019 3:43 am

As Richard said, you can, for example:

Code: Select all

sort tData1 by item 3 of each -- or item 2 or 3 or whatever of each
Which will give you (n this case) all the thicknesses collected together. You can then do lots of stuff to that newly ordered list. For example (pseudo):

Code: Select all

sort yourList by item 3 of each
   repeat with y = 1 to the number of lines of yourList
      if item 2 of line y of yourList = item 2 of line (y + 1) of yourList then next repeat
      else put y & return after lineGroups
   end repeat
end mouseup
This will give you a list delineating the groups of lines that have similar data in item 3. You can then take that data and use it to isolate groups of like minded lines.

There are many ways to do this sort of thing. One day we will discuss arrays...

Craig

hpsh
Posts: 43
Joined: Tue Aug 25, 2015 8:06 pm

Re: Sorting data question

Post by hpsh » Wed Jun 19, 2019 8:05 am

just wonder, is that you want to do to split the data, or to sort the data?

mvh
hpsh

bogs
Posts: 3548
Joined: Sat Feb 25, 2017 10:45 pm

Re: Sorting data question

Post by bogs » Wed Jun 19, 2019 9:10 am

hpsh wrote:
Wed Jun 19, 2019 8:05 am
just wonder, is that you want to do to split the data, or to sort the data?
That, and what is
DavJans wrote:
Tue Jun 18, 2019 11:55 pm
I am having a hard time figuring out how to sort and comment on data.
As Richard and Craig pointed out, sorting is easy, so is splitting up the data. From the format you present, it appears to be a CSV data set -

Code: Select all

Shape,Grade,Thicknes,Dimensions,PieceMark
PL,A36,1/4,1' 9-3/4 x 12' 0-1/2,p2103
PL,A36,1/4,4' 3-3/4 x 5' 6-1/2,p2104
PL,A36,3/16,11' 7-3/4 x 30' 6-1/2,p2105
PL,A36,3/16,1' 8-1/2 x 9' 0-1/2,p2106
PL,A36,1/4,9' 0-7/8 x 4' 0-1/2,p2107
To LiveCode, this is the default itemDelimiter, so each line is already set to be split up into your tData1, tData2, tData(X), etc..., most likely with a repeat loop for each line, something like (not tested):

Code: Select all

repeat for each line x in tData
	if x is line 1 of tData then
		// pass it....
		next repeat
	else
		put item 1 of x into tData1
		put item 2 of x into tData2
		put item 3 of x into tData3
	// etc...
end repeat
Not elegant, I grant you, but it will certainly accomplish splitting up the data. You might also want to look up 'split' and 'combine' in the dictionary, or in the lessons on LiveCode's site. Alternately, you could/might want to plop it all into an array.

As for sorting, you could as Craig mentions sort by item, or sort the whole container without splitting anything (look up 'sort container' in the dictionary).

You didn't mention displaying it, so I assume you already have a method you would like to use for that, but something you may want to think about *before* the sorting is skipping the first line, which looks like the header column for the file (eg. line 1 of the code example).
Last edited by bogs on Wed Jun 19, 2019 9:14 am, edited 1 time in total.
Image

SparkOut
Posts: 1983
Joined: Sun Sep 23, 2007 4:58 pm

Re: Sorting data question

Post by SparkOut » Wed Jun 19, 2019 9:13 am

Looks like "grouping" is the desired outcome.

As Craig mentioned above, sort by item <n> is simple. It is also non-destructive, and can be used iteratively to sort a previously sorted list into groups. This is probably your simplest way to tackle this at the moment.

viewtopic.php?f=6&t=16837#p85252

Arrays are a good subject, but they can be for "another day".

PS @Craig, I found your hymn sheet, did you want it back?

DavJans
Posts: 240
Joined: Thu Dec 12, 2013 4:21 pm
Location: Spokane, WA USA

Re: Sorting data question

Post by DavJans » Wed Jun 19, 2019 2:32 pm

Le me just say thank you for all the responses and I'm sorry I wasn't clear enough and I could have made it much easier.

lets try this.

tData contains this in it:

1
2
3
2
5
6
1
2

Code: Select all

repeat for each line tLine in tData
SWITCH tLine
case 1
if tData1 is empty then
put tLine into tData1
else
put cr & tLine after tData1
end if
BREAK
case 2
if tData2 is empty then
put tLine into tData2
else
put cr & tLine after tData2
end if
BREAK
.....
end SWITCH
Do that is easy and allows me to sort the data into separate variables based on what the number is.

What I am having a hard time figuring out is how to do the same thing, however I don't know what the value is going to be in advance and I don't know how many different values there are going to be.

Each time I run the script there may only be 3 different values or there may be 100.

You asked what I meant about commenting. Basically, after sorting the data I would display it 1 at a time and the user would add a comment to each line:

tData1
1,This is a 1
1,This is also a 1

This is overly simplified, In reality we need to comment weather to use new stock material, or this has been subbed out or material is ordered, arriving 6/23.
"Det bästa stället att hitta en hjälpande hand är i slutet av din egen arm"

bogs
Posts: 3548
Joined: Sat Feb 25, 2017 10:45 pm

Re: Sorting data question

Post by bogs » Wed Jun 19, 2019 3:06 pm

Well, from the code you posted, I would scrap the case/switch, setup a variable to hold an increasing number which can be applied for each repetition, and then proceed to put tLIne(var) after tData(var). That would eliminate worrying about how many lines you have, since it would increase the count until you run out of lines automatically.

Either that, or get the lines in advance, using something like "put the number of lines of tData into someVar", and again skip the case/switch setup since you now have exactly how many lines there are.

Adding the comments should be no great chore, if they are being added on the fly by a user into a field or whatever, just append them to the end of the tLine.

Code: Select all

if tData1 is empty then
	put tLine & "," & tComment into tData1
else
	put cr & tLine & "," & tComment after tData1
end if
...or I am still missing it.
Image

DavJans
Posts: 240
Joined: Thu Dec 12, 2013 4:21 pm
Location: Spokane, WA USA

Re: Sorting data question

Post by DavJans » Wed Jun 19, 2019 3:33 pm

no, you aren't missing it, and I was just using the switch as a way to show what I was trying to get as a result. I guess my real problem is how to create tData(var) and then call them again later.
"Det bästa stället att hitta en hjälpande hand är i slutet av din egen arm"

bogs
Posts: 3548
Joined: Sat Feb 25, 2017 10:45 pm

Re: Sorting data question

Post by bogs » Wed Jun 19, 2019 4:50 pm

Oh, well for that I'd use a script local to hold tData at the top of the script, which will hold the data for multiple handlers (but I'm sure you already know that).

For what I understand to be presented here, I'd roll with something like ...

Code: Select all

local tData

on (put lines into var handler)
	put the lines of [x] into tData
	put the number of lines into tDataCount
end (put lines into var handler)

on (your repeat handler)
	repeat with x=1 to tDataCount
		(your code reduced to an if/then/else statement)
# you can use whatever number x is at to construct the right line...
	end repeat
	put "" into tData; put "" into tDataCount
on (your repeat handler)
...but I'm simple that way.
Image

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5042
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Sorting data question

Post by jacque » Wed Jun 19, 2019 4:59 pm

I'm still not sure what you need. Will you always be concerned about only the first 3 items in the line, or do you need to look at all of them? In two theoretical lines, if the first items are identical and second items are not, which variable would the lines go into? What if only the third items differ?
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

DavJans
Posts: 240
Joined: Thu Dec 12, 2013 4:21 pm
Location: Spokane, WA USA

Re: Sorting data question

Post by DavJans » Wed Jun 19, 2019 5:18 pm

Jacque,

Each line starts with 3 things Shape, Grade and Thicknes, what I really want is to take 1 list and turn it into however many lists needed based on how many different combinations of Shape, Grade and Thicknes.

if I have a list of 10 lines and there are 3 combinations of Shape, Grade and Thicknes. Then I would need 3 different lists.

If I have a list of 1000 lines and there are 34 different combinations of Shape, Grade and Thicknes then I need 34 lists as the output. I can go in and define 1000 variables and for each combination ahead of time and then use a switch statement I just feel that there has to be a better way.

repeat for each line tLine
put item 1 of tLine & item 2 of tLine & item 3 of tLine into tTempName
if variable tData(tTempName) doesnt exist create it and put tLine in it
else
put cr & tLine after it
end if
end repeat

After that I need to display the data of each of these dynamically create d variables to that I can add comments to it.
"Det bästa stället att hitta en hjälpande hand är i slutet av din egen arm"

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5042
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Sorting data question

Post by jacque » Wed Jun 19, 2019 7:24 pm

Thanks, I get it now. I think the best way to approach this would be an array. Try this:

Code: Select all

on sortData
  put fld 1 into tData
  repeat for each line l in tData
    put item 1 to 3 of l into tKey
    put l & cr after tSortArray[tKey] 
  end repeat
end sortData
This will automatically create the number of "variables", which are actually the keys of the array. If you need to know how many and what the keys are, "get the keys of tSortArray" which will give you a return-delimited list. To retrieve one of the sorted content:

get tSortArray["PL,A36,1/4"]

Hopefully this is what you had in mind.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

DavJans
Posts: 240
Joined: Thu Dec 12, 2013 4:21 pm
Location: Spokane, WA USA

Re: Sorting data question

Post by DavJans » Wed Jun 19, 2019 10:27 pm

jacque,

That is perfect and exactly what I needed. To you and everyone else that helped I thank you very much and Im sorry I was so bad at describing what I needed.
"Det bästa stället att hitta en hjälpande hand är i slutet av din egen arm"

hpsh
Posts: 43
Joined: Tue Aug 25, 2015 8:06 pm

Re: Sorting data question

Post by hpsh » Thu Jun 20, 2019 1:58 pm

just for fun

really not great, just a pretty quick thing
Attachments
listsplitter3.zip
(1.84 KiB) Downloaded 12 times

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”