Lookup table with split command not working?

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
glsmith7
Posts: 5
Joined: Thu Dec 07, 2017 10:19 pm

Lookup table with split command not working?

Post by glsmith7 » Tue May 24, 2022 3:59 am

This feels like it should be very simple, but I'm banging my head.

I want to take an good old-fashioned tab-separated set of values with returns at the end of each line, like the following table:

#########################################
# Type # a # b # c #
#########################################
# fruit # apple # banana # cherry #
#########################################
# veggie # avocado # bean # corn #
#########################################
# person # Andy # Barb # Charlie #
#########################################

I want to put it into tArray, and then split the tArray such that I get:

tArray["fruit"]["a"] = apple
tArray["person"]["c"] = Charly

etc.

It would seem like the split command is made to do exactly this. This is what I have:

Code: Select all

on mouseUp

   set the text of field "Table" to empty
   
   # creates the data just so I know I'm not crazy
   
   put "Type" & tab & "a" & tab & "b" & tab & "c" & return into field "Table"
   put "fruit" & tab & "apple" & tab & "banana" & tab & "cherry" & return after field "Table"
   put "veggie" & tab & "avocado" & tab & "bean" & tab & "corn" & return after field "Table"
   put "person" & tab & "Andy" & tab & "Barb" & tab & "Charly" after field "Table"
   
   # the action begins here
   
   put the text of field "Table" into tArray  # puts it into variable. This works as expected, one record per line, tabs between.
   
  split tArray with return and tab # <-- This seems like it should do what I want. But I get a blank tArray array.
 
 end mouseUp
I've also tried

Code: Select all

set the columnDelimiter to tab
set the rowDelimiter to return
   
split tArray with row and column
But to no avail.

Code: Select all

split tArray with return
works as expected, and gives me:

tArray[1] = Type a b c
tArray[2] = fruit apple banana cherry
tArray[3] = veggie avocado bean corn
tArray[4] = person Andy Barb Charly

I suppose I could iterate through each tArray[n] but according to the dictionary this should work, and I presume split would be much faster than me doing that.

It seems like this should be simple, but I've been pounding my head against it.

Thanks for any insights. (If it matters, am in Windows environment, using version 9-6-7.

GLS

stam
Posts: 2682
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Lookup table with split command not working?

Post by stam » Tue May 24, 2022 7:27 am

Possibly the issue is that you are not using good old fashioned tab separated values, where the column name(s) would be in in a row of their own, not in the same line as the data to be read.
Think of TSV like a spreadsheet where the column names are in the top row.
And I don’t think split creates multidimensional arrays…

You could loop through this data structure putting word 1 of each line in to a key of array and split(word 2 to -1) of of each line into the data for that key, which would produce a numerically indexed subarray, or just set the itemdel to tab and with a nested loop just add values as you intend…. The latter would give you what you need above.

Alternatively maybe consider generating the textual version of your data as JSON for easy conversion to/from arrays as you intend…

Of course I could be completely wrong and there is a way for split to churn out multidimensional arrays!

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

Re: Lookup table with split command not working?

Post by jacque » Tue May 24, 2022 8:41 pm

I didn't see any way to completely avoid looping, but there's this:

Code: Select all

on mouseup
  put line 2 to -1 of fld 1 into tList -- assuming line 1 is the header
  split tList by cr and tab
  set the itemDel to tab
  repeat for each key k in tList
    repeat for each item i in tList[k]
      put i into tList[k][char 1 of i]
    end repeat
  end repeat
end mouseup
I know there are warnings about re-using loop variables, but Mark Waddingham said he fixed that quite a while ago. I usually still avoid it, but in this case it seems to work just fine.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

stam
Posts: 2682
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Lookup table with split command not working?

Post by stam » Tue May 24, 2022 8:45 pm

It depends on the source of the text data...
If it can be easily formatted as JSON objects/arrays it's a simple matter of converting to multidimensional arrays in one command as well as the reverse.
After all, JSON was pretty much invented for this kind of thing!

Otherwise as Jacque says... loop ;)

glsmith7
Posts: 5
Joined: Thu Dec 07, 2017 10:19 pm

Re: Lookup table with split command not working?

Post by glsmith7 » Tue May 24, 2022 8:51 pm

Thanks to all. At least it was impossible as i was doing it.

Looks like I need to get into JSON .... :-)

glsmith7
Posts: 5
Joined: Thu Dec 07, 2017 10:19 pm

Re: Lookup table with split command not working?

Post by glsmith7 » Tue May 24, 2022 8:56 pm

For those who might search this later, I was going to post my commented code that has subroutines to do this (though not as elegantly as Jacques above). May help learners besides me.

However, when I try to include it, the board says "You can’t post image, email or url links that are external to this domain. Please remove i."

Didn't have any links, but doesn't like my code. If I figure this out, will post.

GLS

stam
Posts: 2682
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Lookup table with split command not working?

Post by stam » Tue May 24, 2022 9:04 pm

Hi GLS,

to post images/URLs you need to have posted a number of messages first (7? 10?).

However when sharing code, it's good practice to copy/paste the code in the message and surround it with [ code] and [/ code]* tags (just select the code you paste in and click the 'code display' button above the message edit box...

Downside is the it doesn't keep the text colouring but it does keep the indentation and is easier for people to view and copy

* spaces added within the square brackets to stop the forum from displaying it as code!

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

Re: Lookup table with split command not working?

Post by FourthWorld » Tue May 24, 2022 9:57 pm

Why does the data need to be in an array structure specifically?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

glsmith7
Posts: 5
Joined: Thu Dec 07, 2017 10:19 pm

Re: Lookup table with split command not working?

Post by glsmith7 » Tue May 24, 2022 10:31 pm

stam wrote:
Tue May 24, 2022 9:04 pm
Hi GLS,

to post images/URLs you need to have posted a number of messages first (7? 10?).

However when sharing code, it's good practice to copy/paste the code in the message and surround it with [ code] and [/ code]* tags (just select the code you paste in and click the 'code display' button above the message edit box...

Downside is the it doesn't keep the text colouring but it does keep the indentation and is easier for people to view and copy

* spaces added within the square brackets to stop the forum from displaying it as code!
I was doing that -- just trying to post code between the tags like I did above. Odd. GLS

glsmith7
Posts: 5
Joined: Thu Dec 07, 2017 10:19 pm

Re: Lookup table with split command not working?

Post by glsmith7 » Tue May 24, 2022 10:36 pm

FourthWorld wrote:
Tue May 24, 2022 9:57 pm
Why does the data need to be in an array structure specifically?
I'm just playing around and had used that sort of structure before--but I'm entirely self-taught except for one Fortran 77 class I took back in the 1990s, so there may be better ways of achieving the same thing. :-)

In this case, I had two data sets -- each shared some "columns" in common, and some different ones.

So, since dataGrids automatically dump whatever array elements you have into proper columns, I thought an easy way would be to just turn both data sets into 2-d arrays, dump into the dataGrid via dgData, pull them out as dgText and they're both sorted in the proper (and same!) order for combining.

eg

Code: Select all

put arrray1 into the dgData of group "Datagrid"
put the dgtext of group "Datagrid" into aVar

put arrray2 into the dgData of group "Datagrid"
put the dgtext of group "Datagrid" into bVar

put return & bVar after aVar

GLS

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”