Using data from a Text field

Creating Games? Developing something for fun?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Nico_C_GHS
Posts: 44
Joined: Wed Jun 12, 2013 2:36 pm

Using data from a Text field

Post by Nico_C_GHS » Tue Mar 11, 2014 3:34 pm

Hello to all reading this :)

Firstly, this will sound stupid because of the nature of the question but trust me; if this was as easy as it should be then I wouldn't be here.

Here's the problem:
I want to move the data from a text field (an output field used to display text (names) and positive integers (scores)) into an array so that it can be sorted and searched.

I assumed it would be as easy as "put the text of fld "output" into myArray" but I was wrong.
When I do this (after checking the "variables" tab on the coding screen) no variables or arrays are created and the sort/search algorithms don't function as they should.

This process works at another stage in my program where I took the score from a text box to be used as a variable. Even on copying and pasting the code (while amending variable names) the code isn't working.

Any help would be particularly useful and I would be extremely grateful as this is the last stage of a long process of creating this software.


Here's my exact code:

Code: Select all

Local tName 
Local tLines 
Local templine
Local tScores


on mouseUp 
   put empty into fld "output2"
   put the text of fld "output" into tScores
  
   ask "Please enter the name you wish to find:"
   put it into tName
   
   
   
   repeat with loop = 1 to the number of  lines of tScores
   put line loop of tScores into tLines
   split tLines 
   if item 1 of tLines[loop] = tName then 
      put "The name" && tName && "was found on line" && loop into field "output2"
   else
      put "The name" && tName && "could not be found!" & CR & "Please make sure you have entered the name correctly" &CR& "or try searching for another name." into fld "output2"
      end if
end repeat
   

end mouseUp
Nico Colarusso | Gracemount High School

splash21
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 369
Joined: Sun Dec 19, 2010 1:10 am
Location: UK
Contact:

Re: Using data from a Text field

Post by splash21 » Tue Mar 11, 2014 4:54 pm

How are the names separated from the scores in the field? If it's a tab character, then the following will put the data into an array;

Code: Select all

put field "Scores" into tScores
split tScores by LF and tab
-- tScores is now any array of names => scores
check 'split' in the dictionary
LiveCode Development & Training : http://splash21.com

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

Re: Using data from a Text field

Post by jacque » Tue Mar 11, 2014 6:01 pm

I'm surprised there was no script error reported.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Nico_C_GHS
Posts: 44
Joined: Wed Jun 12, 2013 2:36 pm

Re: Using data from a Text field

Post by Nico_C_GHS » Thu Mar 13, 2014 2:47 pm

Okay, I've changed the code. It now goes like this (this has been copy & pasted so there's no difference between what I have and what you see) :

Code: Select all

Global tScores

on mouseUp 
   Local tName
   put empty into fld "output2"
   put fld "output" into tScores
   split tScores by LF and TAB
   
   ask "Please enter the name you wish to find:"
   put it into tName
   
   
   
   repeat with loop = 1 to the number of  lines of tScores
      set the itemdel to TAB
      if item 1 of tScores[loop] = tName then 
         put "The name" && tName && "was found on line" && loop into field "output2"
      else
         put "The name" && tName && "could not be found!" & CR & "Please make sure you have entered the name correctly" &CR& "or try searching for another name." into fld "output2"
      end if
   end repeat
   
   
end mouseUp
The problem I have is that it still isn't displaying the data I've searched for in the output field "output2".

I have attached a screenshot of what the Variables tab has listed for each of the variables.
The problem I seem to be having now is that the name being searched for (which goes into the variable "tName") is being displayed as "tNames" rather than the name I entered.

Am I making a really simple and stupid mistake that I've just overlooked or is there something else wrong here?

Sorry if this is a nuisance but I am grateful for any and all advice given.
Thank you,
Nico
Attachments
Screen Shot 2014-03-13 at 13.44.21.png
Screenshot of the "Variables" tab on the coding screen
Nico Colarusso | Gracemount High School

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

Re: Using data from a Text field

Post by jacque » Thu Mar 13, 2014 5:47 pm

Items are character-delimited lists (usually commas, but tabs in this case) and the scores array doesn't have any of those, so you don't want to use "items". Or if you do, then don't turn the variable into an array. If you don't use split, item 2 of the line will be the score and item 1 will be the name. The easiest thing in this case is just to remove the split command and inspect each line, unless you'll need an array for more complex storage later.

If you do use split, then the name will be in the keys of the array and you don't need to loop through the lines, you can just get the value directly:

Code: Select all

split tScores by LC and tab
put the keys of tScores into tNameList
if tName is among the lines of tNameList then
  put tScores[tName] into tScore -- will contain the numerical score
end if
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Nico_C_GHS
Posts: 44
Joined: Wed Jun 12, 2013 2:36 pm

Re: Using data from a Text field

Post by Nico_C_GHS » Mon Mar 17, 2014 9:51 am

Hey Jacque,

I'll try the code you've advised and see if it makes a difference :)

Will this code allow me to display the Name and score of a user or will it just display the name? ( I also need the data split so that it can be sorted using my adaptation of the normal LiveCode sorting algorithm).
Nico Colarusso | Gracemount High School

Nico_C_GHS
Posts: 44
Joined: Wed Jun 12, 2013 2:36 pm

Re: Using data from a Text field

Post by Nico_C_GHS » Mon Mar 17, 2014 10:23 am

Hi, using the code you recommended I managed to adapt the function to work properly, thank you very much :)

Is it possible to use a similar method to split the names from the scores so that I can sort the scores? And will I be able to sort the scores and keep the corresponding names with the scores when displayed (after being sorted)?
Nico Colarusso | Gracemount High School

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

Re: Using data from a Text field

Post by jacque » Mon Mar 17, 2014 6:42 pm

Since your data is already tab-delimited you really don't need arrays, and converting to an array is just adding an unnecessary step. Tab-delimited lists are perfect for LiveCode as-is.

Assuming you want to show names and scores, sorted with the highest scores at the top, just do this:

Code: Select all

set the itemDelimiter to tab
sort tScores descending numeric by item 2 of each
put tScores into fld "display"
If you have a field named "display" with tabstops set to something that looks good, you've got everything you need in three lines of script. See the "sort" command in the dictionary.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Nico_C_GHS
Posts: 44
Joined: Wed Jun 12, 2013 2:36 pm

Re: Using data from a Text field

Post by Nico_C_GHS » Tue Mar 18, 2014 2:51 pm

Thank you! I appreciate the help, this was instrumental in me completing this on time. I've implemented the code and I feel as if it is finished (finally) :D ! Thanks again,
Nico.
Nico Colarusso | Gracemount High School

Post Reply

Return to “Games”