Page 1 of 2
Limit put keys of array to first level?
Posted: Wed Feb 26, 2014 3:36 am
by trenatos
I have a multidimensional array and need to list only the items in the first level.
Here's the overview of the array
channels[channelname][messages]
so for example channels["bobs channel"]["firstmessage\NLsecondmessage\NLetc"]
channels["lucys channel"]["firstmessage\NLsecondmessage\NLetc"]
put the keys of channels works but displays both channelnames and messages, is there a simple way of limiting this so it only spits out the channelname?
Re: Limit put keys of array to first level?
Posted: Wed Feb 26, 2014 4:18 am
by dunbarx
Hi.
You can always massage the data in the clear. Combine the array variable with, say, return and tab. Set the itemDel to tab, and then extract the second item in each line of the (now ordinary) new variable into a return delimited list, or whatever.
Craig Newman
Re: Limit put keys of array to first level?
Posted: Wed Feb 26, 2014 4:45 am
by trenatos
That doesn't seem to play nice with the multidimensional array.
I was getting a bunch of blank lines, and only one result, so I tried with a simple test and when it's a multidimensional array it doesn't work right.
I tried:
Code: Select all
put "stuff1" into bob["bob"]
put "stuff2" into bob["bob2"]
combine bob with ","
put Bob
The above works fine, but this does not:
Code: Select all
put "stuff1" into bob["bob"]["messages"]
put "stuff2" into bob["bob2"]["messages"]
combine bob with ","
put Bob
Re: Limit put keys of array to first level?
Posted: Wed Feb 26, 2014 5:21 am
by Simon
Funny just posted this.
http://forums.runrev.com/phpBB2/viewtop ... =7&t=19331
You would change to ["channelname"]
Simon
Re: Limit put keys of array to first level?
Posted: Wed Feb 26, 2014 5:33 am
by trenatos
I'm sorry, but I don't understand how that would help me.
I'm not pulling the data from a datagrid, so I can't use dgIndexes.
Assume that "bobs channel" and "lucys channel" are unknown names.
It can't be this hard to just pull all the first keys from an array, it's driving me insane. lol
All I need to figure out at this stage is how to get all the values of the first keys of channel[][]
Re: Limit put keys of array to first level?
Posted: Wed Feb 26, 2014 5:38 am
by Simon
Hi sorry, but you see theDataA is a multi-dimentional array

Ahh...
Code: Select all
repeat for each key tKey in theDataA
put theDataA[tKey]["channelname"] & cr after tNames
end repeat
There does that help?
Simon
Re: Limit put keys of array to first level?
Posted: Wed Feb 26, 2014 5:48 am
by trenatos
To a point, there's a misunderstanding, it's likely due to my stupid naming-scheme.
I'm dynamically creating keys in the array based on user input basically like this;
User joins channel "bobs cooking", so a new value for the channel array is created and looks like this: put "bobs cooking" into channel["bobs cooking"]
Subsequent messages from that channel are put into the array like this: put "the message text" into channel[currentChannel]["messages"]
Where currentChannel is, well the current channel the user is chatting on.
I was under the assumption that I would easily be able to retrieve only the first keys in order to create a list of channels.
Re: Limit put keys of array to first level?
Posted: Wed Feb 26, 2014 6:00 am
by Simon
Hi Marcus,
Easier still
Code: Select all
on mouseUp
put "bobs cooking" into channel["bobs cooking"]
put "the message text" into channel["bobs cooking"]["messages"]
put "sue cooking" into channel["sue cooking"]
put "the message text" into channel["sue cooking"]["messages"]
put "tom cooking" into channel["tom cooking"]
put "the message text" into channel["tom cooking"]["messages"]
repeat for each key tKey in channel
put tKey & cr after tChannel
end repeat
end mouseUp
Did I get the array right?
Simon
Edit: I see I could do without the " put "bobs cooking" into channel["bobs cooking"]" part.
Re: Limit put keys of array to first level?
Posted: Wed Feb 26, 2014 6:09 am
by trenatos
See? I KNEW there had to be a simple way
Thanks, Simon, if we ever meet I owe you a handful of beers by now. lol
Re: Limit put keys of array to first level?
Posted: Wed Feb 26, 2014 6:19 am
by jacque
trenatos wrote:put the keys of channels works but displays both channelnames and messages, is there a simple way of limiting this so it only spits out the channelname?
I'm not seeing that, the keys of a test array return only the first level. I have an array like this:
Code: Select all
PeopleArray
Person1
First name
Last name
DOB
Person2
First name
Last name
DOB
"Put the keys of peopleArray" returns:
Person1
Person2
Re: Limit put keys of array to first level?
Posted: Wed Feb 26, 2014 6:26 am
by dunbarx
He was trying to get deeper "levels" of an array.
Code: Select all
on mouseUp
repeat with x=1 to 10
repeat with y=1 to 10
repeat with z=1 to 10
put random(433) into myArray[x][y][z]
end repeat
end repeat
end repeat
repeat for each key tKey in myArray
put myArray[tKey][3][4] & return after tResults
end repeat
answer tResults
end mouseUp
This lists the third nesting (the "Z"), key by key. But defined by the hard wired "3" and "4" in the first and second levels.
I think. Anyway, Simon seems to have hit a chord.
Craig
Re: Limit put keys of array to first level?
Posted: Wed Feb 26, 2014 6:33 am
by trenatos
Yes, I have a multidimensional array with an "unknown" first key (It's unknown by the function because I don't want to maintain a lookup-list).
The function needed to pull the name of the first key, IE. channels[thiskey][notthisone] without knowing the name of that first key.
I couldn't solve it, been trying for hours, but Simon solved it for me, he's solved a bunch of stuff for me in the past too, ergo the beer

Re: Limit put keys of array to first level?
Posted: Wed Feb 26, 2014 7:00 am
by trenatos
If anyone's curious of what I'm doing with it, I'm building an IRC client entirely in LiveCode.
At this point (With Simons help above) I currently have: Connecting and authenticating with server, multi-channels, shorthands for join and part, userlist and channel list.
Next up will probably be a settings tab and more short commands.
Re: Limit put keys of array to first level?
Posted: Wed Feb 26, 2014 7:49 am
by jacque
Glad you got it working. As far as I can see, "the keys" and Simon's method both return the same thing: the first level of the array. I probably missed something.
Re: Limit put keys of array to first level?
Posted: Wed Feb 26, 2014 8:18 am
by Simon
Yeah, Jacque.
That is what I'm seeing as well. Not clear why "put the keys" is not working.
Simon