Page 1 of 1

Dealing with sub-items within items

Posted: Thu Nov 03, 2016 6:11 pm
by ittarter
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?

Re: Dealing with sub-items within items

Posted: Thu Nov 03, 2016 6:43 pm
by shaosean

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..

Re: Dealing with sub-items within items

Posted: Thu Nov 03, 2016 10:07 pm
by dunbarx
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

Re: Dealing with sub-items within items

Posted: Fri Nov 04, 2016 9:53 am
by ittarter
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.

Re: Dealing with sub-items within items

Posted: Fri Nov 04, 2016 9:56 am
by ittarter
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.

Re: Dealing with sub-items within items

Posted: Sat Nov 05, 2016 9:10 pm
by [-hh]
Another option could be to use the TWO delimiters that are available
the linedelimiter and the itemdelimiter.

Re: Dealing with sub-items within items

Posted: Tue Nov 08, 2016 1:28 pm
by MaxV
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

Re: Dealing with sub-items within items

Posted: Tue Nov 08, 2016 3:04 pm
by dunbarx
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