Problem w/ nested property lists?

Here is some example code for a problem I am having w/ ST:


set foo to {
   bar:{
      foo1:bar1,
      foo2:bar2,
      foo3:bar3
   }
}

// Why do the first and last items have a parenthesis in them?
repeat with each item of (bar) of (foo)
   put it
end repeat

// Works
put property (bar) of (foo) into newfoo1
put newfoo1

// Throws exception
add foo.bar to newfoo2
put newfoo2

Here is a more specific example of what I am trying to do (scratch.script):


set foobar to new scratch with (mybar:"bar_2")
run foobar.testFoo2

to initialize

	add properties of this object to me
	
	set my foo to {
		bar_1:{
			foo1:bar1,
			foo2:bar2,
			foo3:bar3
		},
		bar_2:{
			foo1:bar4,
			foo2:bar5,
			foo3:bar6
		}
	}
	
	// If I do this then I get an exception
	add (my mybar) of (my foo) to me

	// Works, but I get an exception that there is no "testFoo2" handler
	//put property (my mybar) of (my foo) into me
	
	put me
	
end initialize

properties
	mybar:"bar_1"
end properties

to handle testFoo2
	put (my foo2)
end testFoo2

[list=1][]Why do I get the extraneous parenthesisi in the first example?
[
]Why do I get an exception when trying to use “add”?
[*]Why is testFoo2 undefined if I use the “put” method?[/list:o]

Thanks!

pv, here are some answers for you:

  1. Extraneous parentheses in items

The first thing to understand is the meaning of the term “item” in SenseTalk. There are two types of items: items in a list and items in text. If you use the generic term “item” (instead of specifying “list item” or “text item”), then SenseTalk assumes you mean “list item” if the value is a list, and “text item” otherwise.

In your case, the value of foo.bar is a property list (which is different from an ordinary list), so “text item” is assumed. Foo.bar’s value as text is <<(foo1:“bar1”, foo2:“bar2”, foo3:“bar3”)>> and “text item” by default means items separated by commas, so the three “items” in this text are:
<<(foo1:“bar1”>>
<foo2:“bar2”>
<foo3:“bar3”)>

I hope that’s all clear.

Since what you’re working with here is a property list, you can step through its entries like this:

repeat with each key of bar of foo
    put "Key=" & it && "Value=" & foo.(it)
end repeat
  1. Exception when using “add”

This issue is a little bit similar. The “add” command (like the “+” operator) performs arithmetic addition, so it’s expecting to see numbers (or equal-length lists of numbers to add their corresponding items). To add a set of properties to another object or property list, you need to use the “add properties” command.

In your example, the command might look like this:

add properties of ((my mybar) of (my foo)) to me

(Note: I’ve added an additional pair of parentheses that is also needed for this to be interpreted correctly. Make two copies of the above command, one without the outer parentheses, and the syntax colorization will show you that they are both valid but have very different meanings. The moral: SenseTalk can be subtle sometimes – syntax colorization is your friend!)

  1. testFoo2 undefined after “put … into me”

This one is interesting: You are essentially self-destructing!

When you call “new scratch” in the first line of your initial handler, a new object is created, with scratch as its helper. This object is then sent an “initialize” message.

Within your initialize handler, the value of “me” is this new object. When you say “put x into me” (whatever x is) you replace the value of “me” with x. In your case, “x” is a property list without any helpers. When the initialize handler ends, this new value of me is returned and put into foobar in your initial handler.

For those of you “following along at home”, you might try adding this as the second line of the initial handler and then try running the script with and without commenting out the “put” line in initialize:

put foobar && "Helped by:" && foobar's helpers

This is also a great place to take advantage of Eggplant’s debugging capabilities to step through the code one line at a time and examine things along the way using put statements in the Do box.

Thanks for the interesting questions, and happy scripting!

Sweet! You’re “add” trick w/ the subtle parentheses worked!
The “put” that I was trying to do was only a bad hack I was trying in place of the “add” that I could earlier not get to work.
I removed the “put” and fixed the “add” and all is well!

The “items” answer was interesting and well appreciated.

Thank you very much for the prompt support!

Pv