Limit put keys of array to first level?

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

trenatos
Posts: 189
Joined: Wed Jul 03, 2013 6:46 am
Location: Virginia, USA
Contact:

Limit put keys of array to first level?

Post by trenatos » Wed Feb 26, 2014 3:36 am

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?
Marcus

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Limit put keys of array to first level?

Post by dunbarx » Wed Feb 26, 2014 4:18 am

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

trenatos
Posts: 189
Joined: Wed Jul 03, 2013 6:46 am
Location: Virginia, USA
Contact:

Re: Limit put keys of array to first level?

Post by trenatos » Wed Feb 26, 2014 4:45 am

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
Marcus

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Limit put keys of array to first level?

Post by Simon » Wed Feb 26, 2014 5:21 am

Funny just posted this.
http://forums.runrev.com/phpBB2/viewtop ... =7&t=19331
You would change to ["channelname"]

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

trenatos
Posts: 189
Joined: Wed Jul 03, 2013 6:46 am
Location: Virginia, USA
Contact:

Re: Limit put keys of array to first level?

Post by trenatos » Wed Feb 26, 2014 5:33 am

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[][]
Marcus

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Limit put keys of array to first level?

Post by Simon » Wed Feb 26, 2014 5:38 am

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

trenatos
Posts: 189
Joined: Wed Jul 03, 2013 6:46 am
Location: Virginia, USA
Contact:

Re: Limit put keys of array to first level?

Post by trenatos » Wed Feb 26, 2014 5:48 am

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

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Limit put keys of array to first level?

Post by Simon » Wed Feb 26, 2014 6:00 am

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.
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

trenatos
Posts: 189
Joined: Wed Jul 03, 2013 6:46 am
Location: Virginia, USA
Contact:

Re: Limit put keys of array to first level?

Post by trenatos » Wed Feb 26, 2014 6:09 am

See? I KNEW there had to be a simple way :D

Thanks, Simon, if we ever meet I owe you a handful of beers by now. lol
Marcus

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

Re: Limit put keys of array to first level?

Post by jacque » Wed Feb 26, 2014 6:19 am

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
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Limit put keys of array to first level?

Post by dunbarx » Wed Feb 26, 2014 6:26 am

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

trenatos
Posts: 189
Joined: Wed Jul 03, 2013 6:46 am
Location: Virginia, USA
Contact:

Re: Limit put keys of array to first level?

Post by trenatos » Wed Feb 26, 2014 6:33 am

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 :P
Marcus

trenatos
Posts: 189
Joined: Wed Jul 03, 2013 6:46 am
Location: Virginia, USA
Contact:

Re: Limit put keys of array to first level?

Post by trenatos » Wed Feb 26, 2014 7:00 am

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

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

Re: Limit put keys of array to first level?

Post by jacque » Wed Feb 26, 2014 7:49 am

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Limit put keys of array to first level?

Post by Simon » Wed Feb 26, 2014 8:18 am

Yeah, Jacque.
That is what I'm seeing as well. Not clear why "put the keys" is not working.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”