Dealing with sub-items within 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
ittarter
Posts: 151
Joined: Sat Jun 13, 2015 2:13 pm

Dealing with sub-items within items

Post by ittarter » Thu Nov 03, 2016 6:11 pm

Hi guys!

I have a text of tab-delimited items. In some of those items, there are alternate sub-items (separated by slash). I want to get a text where only one sub-item per tab-delimited item is selected.
Sample:

Code: Select all

put "326	I'm OK	Je vais bien	Aussi	Merci	Savoir	Grand	It is/Il est/Elle est	Savoir/Connaître" into tWordList
set the itemdel to tab
put 0 into tItemNumber
repeat for each item tItem in tWordList
      add 1 to tItemNumber
      if tItem contains "/" then
         set the itemdel to "/"
         put any item of tItem into tSelected
         put tSelected into item tItemNumber of tWordList
         set the itemdel to tab
      end if
   end repeat
Why isn't my code working, or is there a better way to do this?

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: Dealing with sub-items within items

Post by shaosean » Thu Nov 03, 2016 6:43 pm

Code: Select all

   put "326	I'm OK	Je vais bien	Aussi	Merci	Savoir	Grand	It is/Il est/Elle est	Savoir/Connaître" into tWordList
   set the itemdel to tab
   put 0 into tItemNumber
   repeat for each item tItem in tWordList
      add 1 to tItemNumber
      if tItem contains "/" then
         set the itemdel to "/"
         put any item of tItem into tSelected
         set the itemdel to tab  // this line moved
         put tSelected into item tItemNumber of tWordList
      end if
   end repeat
   put tWordList
looks like you were switching back to the TAB itemDelimiter too late..

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

Re: Dealing with sub-items within items

Post by dunbarx » Thu Nov 03, 2016 10:07 pm

Hi.
In your original text, I only see one item. It is hard to distinguish tabs from spaces, which makes it difficult to "read" your text, or to parse it visually.

Anyway, try this. Much depends on getting around the LC restriction (ptui) of not being able to access a "large" chunk inside a "small" one. Make a button and a field. In the button script.

Code: Select all

on mouseUp
   put "A,B,C,D/d/dd,E,F/f/ff/fff,G,H," into tWordList
   set the itemDel to comma ---not really required here, eh?
   
   repeat with y = 1 to the number of items in tWordList
          put item y of tWordList into line y of temp
   end repeat
   
   set the itemDel to "/"
   repeat with y = 1 to the number of lines of temp
      if line y of temp contains "/" then
         put item 2 to 100 of line y of temp into foo
         put item 1 of line y of temp & "/" & any item of foo into line y of temp
      end if
   end repeat
   put temp into fld 1
end mouseUp
Click rapidly and watch the field.

Now this is verbose, though not fully of its own fault (see spitting above) and old-fashioned. It begs for an array. How are you with arrays? They cut through that spitting thing.

Craig

ittarter
Posts: 151
Joined: Sat Jun 13, 2015 2:13 pm

Re: Dealing with sub-items within items

Post by ittarter » Fri Nov 04, 2016 9:53 am

Thanks, shaosean! You are right :) It still has small hiccups on the last item of a line sometimes, but it's better.

I'm using tab delimited because I'm pulling data from a spreadsheet and I don't want to use a character like comma or slash because sometimes there are commas and slashes in the data. I know that visually it's a pain in the ass.
Last edited by ittarter on Fri Nov 04, 2016 9:59 am, edited 1 time in total.

ittarter
Posts: 151
Joined: Sat Jun 13, 2015 2:13 pm

Re: Dealing with sub-items within items

Post by ittarter » Fri Nov 04, 2016 9:56 am

dunbarx wrote: Now this is verbose, though not fully of its own fault (see spitting above) and old-fashioned. It begs for an array. How are you with arrays? They cut through that spitting thing.
Hi Craig :)

I've never used an array. I know they're really useful and important but also difficult to understand for non-mathematic types. I'd love to start using them but don't really know where to start.

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Dealing with sub-items within items

Post by [-hh] » Sat Nov 05, 2016 9:10 pm

Another option could be to use the TWO delimiters that are available
the linedelimiter and the itemdelimiter.
shiftLock happens

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: Dealing with sub-items within items

Post by MaxV » Tue Nov 08, 2016 1:28 pm

ittarter wrote:
dunbarx wrote: I've never used an array. I know they're really useful and important but also difficult to understand for non-mathematic types. I'd love to start using them but don't really know where to start.
You can read here: http://livecode.wikia.com/wiki/Arrays
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

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

Re: Dealing with sub-items within items

Post by dunbarx » Tue Nov 08, 2016 3:04 pm

Try this handler in a button:

Code: Select all

on mouseUp
   repeat 20
      put any word of "cat dog flea mouse pony hafnium" & return after temp
   end repeat
   breakPoint
   repeat for each line tLine in temp
      add 1 to wordCount[tLine]
   end repeat
   combine wordCount by return and comma
   answer wordCount
end mouseUp
When the handler stops, open the variable "temp" in an external window so you can see each line in turn. Step through and watch the array build in the debugger.

An array variable is a multi dimensional gadget that can hold many data associations, as opposed to just a single association. This gives one enormous power and compactness. The only downside is that the information within is not visible "in the clear" as I call it, unless that variable is deconstructed with the "combine" command, or, as you just did, observed in the debugger.

So the array variable "wordCount" holds pairs of data associations, as you have seen. Otherwise, a table, or perhaps several explicitly created variables would have to be set up to do the same job. No problem, just not as compact. You can go even farther with these, once you get going.

Craig

Post Reply