sorting data using delimiters and managing items

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
ThomasBodetti
Posts: 16
Joined: Tue May 14, 2013 12:30 pm

sorting data using delimiters and managing items

Post by ThomasBodetti » Tue May 14, 2013 12:41 pm

I am attempting to manage data in an app, where I can be reasonably sure that the data I want is being processed and stored where it should be stored.

If I set the itemDelimiter to * or any character not expected to be part of the data,

How would I be able to identify items, (text examples show below) so that I could make use of that data in the script.

*
F1
Jane saw something move in the field across the way
she walks over to look and discovers it is a skunk.
she finds something buried in the field.
she looks across at the field but decides to do nothing.
*
F2
Jane realizes too late that the truck approaching the intersection is not going to stop
she stomps on the brakes and the front tire blows out.
she was not paying attention and realizes that she too cannot stop.
she was going slow enough to stop in time but the truck still hits her car.
*
F3
Jane feels disorganized most of the time
she does not get enough sleep at night.
she is not taking her medication on a regular basis.
she finds out that she has a rare medical condition that effects her sleep.
*

The idea here is to of course have five lines of text, as an item, using the first line to identify the nature of the data, (in this case it is a female named jane)

So I attempt to set the itemDelimiter to * then I, want to sort that data, but I want to get rid of the delimiters,

I attempted to put empty into the first but that did not work,

so I am stuck at sort,

set the itemDelimiter to "*"
sort items of incommingSnippets #how to get rid of the delimiter needed to seperate the different chunks of text, If I sort by line one of each I will then have a line where I can describe the data to be used in a container,
put incommingSnippets into fld "testing"

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: sorting data using delimiters and managing items

Post by Dixie » Tue May 14, 2013 1:27 pm

replace "*" with empty inThePlaceIamStoringTheText

I've just read your post a little closer... does F1 mean that these lines are coming from fld 1 ?... Once you have them in a variable or a fld, then exactly how do you want to sort them ?

Dixie

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

Re: sorting data using delimiters and managing items

Post by sturgis » Tue May 14, 2013 2:49 pm

If f1, f2,f3 are designating a record, and you want to be able to put those records in order by name (IE sort f2,f3,f1 to f1,f2,f3) it might be easier to use an array.

First take your current data and put it into a variable.
Set the itemdelimiter (since you apparently already have one set up as *)
Then cycle through the data 1 item at a time and put stuff into an array keyed by your record designator.

Code: Select all

-- if the delimited data is in variable myVariable
-- and myVariableA is set as a persistant variable (could be a script local, or a global)
set the itemdelimiter to "*" -- set the delimiter

repeat for each item tItem in myVariable
   put line 1 of tItem into tKey -- grab the first line of the item and use it as the key (f1,f2,f3)
   put tItem into myVariableA[tKey]
end repeat
-- all the data is now in the array variable myVariable keyed by the first line of each item

--To get the data back out in sorted order put the keys into a variable, and sort them.
-- then use the sorted list to get the items back out. 
-- could be done with a repeat loop or whatever.  To get the 3rd item out you would access it with myVariableA["f3"] or a keyvariable that evaluates to f3 of course. 



ThomasBodetti
Posts: 16
Joined: Tue May 14, 2013 12:30 pm

Re: sorting data using delimiters and managing items

Post by ThomasBodetti » Wed May 15, 2013 12:29 pm

Thank you for your reply, Yes the first line would be a designation for the type of data later I would probably add more information.

But yes both of those work quite well, I am a little amazed at how easy it is to use a few words and accomplish so much, again thank you for your reply and solution.

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

Re: sorting data using delimiters and managing items

Post by sturgis » Wed May 15, 2013 3:07 pm

Just noticed I shoved the entire item into the array (including the f1,f2,f3 stuff) if you don't want to include that in the actual data, (since it is already keyed by the f1,f2,f3) instead of:

Code: Select all

   put tItem into myVariableA[tKey]
you can do:

Code: Select all

   put line 2 to -1 of tItem into myVariableA[tkey] -- start at line 2 to the last line of the item

ThomasBodetti
Posts: 16
Joined: Tue May 14, 2013 12:30 pm

Re: sorting data using delimiters and managing items

Post by ThomasBodetti » Wed May 15, 2013 5:52 pm

Ok that makes sense, I tried out a couple of different things,

on mouseUp
put the text of fld "testing" into mySnippetVar
set the itemdelimiter to "*"
repeat for each item tItem in mySnippetVar
put line 2 to -1 of tItem into mySnippetVar[tkey]
end repeat

put mySnippetVar into fld "testing2" -- generates an error, button "ButtonB": execution error at line 8 (Chunk: can't store to destination container), char 21
--put tItem into fld "testing2"
end mouseUp

When I put tItem into fld "testing2" it lists the last item, guess I need to have a closer look at how to list the items and then call them by the key,

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: sorting data using delimiters and managing items

Post by Klaus » Wed May 15, 2013 6:11 pm

Hi Thomas,

an ARRAY is a "construct" that cannot be displayed in a field, hence the error! :-)
You will need to convert it back to a string to display its content (and keys) in a field.


Best

Klaus

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

Re: sorting data using delimiters and managing items

Post by sturgis » Wed May 15, 2013 6:16 pm

ThomasBodetti wrote:Ok that makes sense, I tried out a couple of different things,

on mouseUp
put the text of fld "testing" into mySnippetVar -- this should be fine
set the itemdelimiter to "*" -- this too
repeat for each item tItem in mySnippetVar -- this too
--at this point the variable tItem (from above) will contain 1 of the items that was in mySnippetVar
--But you forgot to grab the first line of tItem to use as the key in the next line
put line 1 of tItem into tKey -- need to put a value into tKey like so.

-- the next problem is that you are taking a variable that is full of data
-- and trying to turn it into an array.
-- strange behavior can occur when you try to have both keys and data on the same array level.
-- Pick a different name. maybe a temporary name like tSnippet
put line 2 to -1 of tItem into tSnippet[tkey] -- now this will work.
-- now line 2 to -1 (2 to the last line) of the item currently being worked on
-- is in tSnippet, keyed by tKey which was also pulled from the item currently beign worked on.
end repeat

-- if mySnippetVar has been declared so that its persistant then we can
put tSnippet into mySnippetVar -- it now contains an array that you can use elsewhere

-- If you want to see the keys that were created, and their values in the message box, you can use the following code:
put the keys of mySnippetVar into tKeys
sort tKeys -- sort them since keys don't necessarily come out the order they go in
put empty -- clear the message box
repeat for each line tLine in tKeys -- since the keys are returned as a return delimited list, cycle through the lines
put "Key: " & tLine && "Contains: " & mySnippetVar[tLine] & cr after msg
end repeat

end mouseUp

When I put tItem into fld "testing2" it lists the last item, guess I need to have a closer look at how to list the items and then call them by the key,

See above changes and comments.

Also note, you might put the handler that creates your array into the card or stack script and just have the button call it. This way you can declare variables local to the card script (script locals) so that several handlers can all see the same variables. That way you can break things into manageable chunks. A handler to parse the data, a handler to pull out the data you need from your array, etc.

Hard to give exact guidance without knowing where you wish to end up, but hopefully this is helpful anyway.

ThomasBodetti
Posts: 16
Joined: Tue May 14, 2013 12:30 pm

Re: sorting data using delimiters and managing items

Post by ThomasBodetti » Wed May 15, 2013 6:21 pm

Yes, that is a good idea, the base idea is to Get, some text data, (flat file) from a remote server,

Post that data into a substack variable, sort it then load it into fields that co-respond to the lines of each item,

item 1
line 1 into fld A
line 2 into fld B
line 3 into fld C
line 4 into fld D

item 2
line 1 into fld A
line 2 into fld B
line 3 into fld C
line 4 into fld D

ThomasBodetti
Posts: 16
Joined: Tue May 14, 2013 12:30 pm

Re: sorting data using delimiters and managing items

Post by ThomasBodetti » Wed May 15, 2013 8:17 pm

Thank you Klaus I will read though the documentation to see how I can find out more about these subjects, I appreciate the information, so in this case I would say that the engine does not know what of the construct to put into the field, so I have to construct a method of telling the engine what I want to put there.

Very interesting

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: sorting data using delimiters and managing items

Post by Klaus » Wed May 15, 2013 9:52 pm

Hi Thomas,

you can do something like this:
...
## convert Array to string:
## Loop through all keys and write the name of the key and ":" and the content of that key and RETURN to a variable...
repeat for each key tKey in theArray
put tKey & ":" && theArray[tKey] & CR after tArray2StringVariable
end repeat

## get rid of last CR
delete char -1 of tArray2StringVariable

##... that you now can display in a field_
put tArray2StringVariable into fld "display the array"
...
Best

klaus

ThomasBodetti
Posts: 16
Joined: Tue May 14, 2013 12:30 pm

Re: sorting data using delimiters and managing items

Post by ThomasBodetti » Wed May 15, 2013 11:14 pm

I played with this for several iterations, producing various results, I am certain I have something a little off, I can almost see where I am going wrong but it just escapes me at the moment.

Code: Select all

on mouseUp
      put the text of fld "testing" into mySnippetVar
      set the itemdelimiter to "*" 
      repeat for each item tItem in mySnippetVar
     put line 2 to -1 of tItem into mySnippetVar[tKey]
end repeat
  
         ## convert Array to string:
## Loop through all keys and write the name of the key and ":" and the content of that key and RETURN to a variable...
repeat for each key tKey in mySnippetVar
  put tKey & ":" && mySnippetVar[tkey] & CR after tArray2StringVariable
end repeat

## get rid of last CR
delete char -1 of tArray2StringVariable

##... that you now can display in a field_

put tArray2StringVariable into fld "testing2"

end mouseUp
The current result is below, I can see that it is printing the variable which should I think be different, somehow

tKey: F4
Jane is trying to find a new job but she
cannot seem to get any time off work so she can go look for work.
finds that there are not many jobs available.
seems to be unable to stay organized and so decides to just keep on working the same job.

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

Re: sorting data using delimiters and managing items

Post by sturgis » Thu May 16, 2013 12:28 am

You have this:

Code: Select all

  
repeat for each item tItem in mySnippetVar
     put line 2 to -1 of tItem into mySnippetVar[tKey]
end repeat
Where does tKey come from? You aren't setting the value of tKey in your loop so the only key you have in there is "tKey" as a string. (the engine makes its best guess, and if tKey has no value and has not been declared the engine decides its a string.)

So each and every loop you are setting 1 array key named "tKey"

Based on your sample data from before your key is the very first line after the * . Though looking at the data again its actually the second line (my mistake, the * is on a line by itself so that counts as 1 line)

So, even if you're setting the value of tKey to "line 1 of tItem" it would be setting the key to the wrong value (basically an empty key which is.. weird)

You can either restructure your data so that it looks like so..

f1
data line
data line
dataline
*f2
data line
data line
data line

or If you really have a preceeding * before the first item, then a) you need to ignore item 1 (which would be the non-existant item before the first *) and start with the second item. and you would want to use line 2 of the current item (rather than item 1 as I originally thought) as your key. And adjust your code so that its like 3 to -1 of the item as the data itself.

If you restructure as above then you can use line 1 as the key (as in the original example) and don't need to worry about the preceeding item that doesnt exist.

Honestly, you could also restructure the data like so:
f1*data line*data line*data line*data line*data line
f2*data line*data line*data line*data line*data line

In this way you can set the itemdelimiter to "*" as before and then just access things by item number of line number..
Having said all this, filling up an array and using that instead would be very useful. (Or even an sqlite database instead?)

I made a very quick example stack you can find here: https://dl.dropboxusercontent.com/u/119 ... e.livecode

On opening the stack if you click "create the array" it does just that. I restructured the data as mentioned to make things easier.

Once the array is created it shows the keys that were created in the bottom left field. If you click one of those keys it shows the lines of text associated with that particular key.

Once the array is created, if you don't want to have to run through the text every time the app starts you can arrayencode() the array and save it to a file or property and then reload it and arraydecode() it the next time the application is run. You could also use subkeys (if you wanted) with your array so that you could for example have myArray["f1"][1], myarray["f1"][2] etc 1 numbered key for each line. so f1 would have subkeys 1,2,3,4,5 each a different line, and f2 would have subkeys 1,2,3,4,5 etc on down the line. Then any time you want to use line 3 of key f2 you can get to it using myArray["f2"][3]

ThomasBodetti
Posts: 16
Joined: Tue May 14, 2013 12:30 pm

Re: sorting data using delimiters and managing items

Post by ThomasBodetti » Thu May 16, 2013 1:05 am

Ok, I believe I understand the way that it works now, that I see the example thank you for sharing your time so generously.

There is a lot to understand but that really helps, when I look at the example I can understand how it should work, so much better again thank you.

Post Reply