Re: multi-dimensional arrays (new to AS)
Re: multi-dimensional arrays (new to AS)
- Subject: Re: multi-dimensional arrays (new to AS)
- From: Graff <email@hidden>
- Date: Tue, 20 Apr 2004 21:45:49 -0400
On Apr 20, 2004, at 2:28 PM, Jeff Hartman wrote:
Bear with me as I am pretty new to applescript. Sorry for terminology
if not
correct.
I have a list containing server volumes. Each server item in the list
contains a list of connection strings. Trying to fit into a Window
environment here...
So:
set theList to {server1:{ipaddress:"x.x.x.x",path:"blah"}, server2:{
....
That isn't an array or a list, it is a record. A record is a series of
key-value pairs, a list is a series of values. You can't iterate
through a record, you need to access each value by supplying the key
that the value is attached to.
For example, this is a list:
set theList to {1, 2, 3, 4, 5}
You would access each item in the list in several ways. One way is
direct access:
set theValue to item 1 of theList
Another is through a repeat statement:
set sum to 0
repeat with theValue in theList
set sum to sum + theValue
end repeat
You could also do this the long way:
set sum to 0
repeat with i from 1 to length of theList
set sum to sum + item i of theList
end repeat
Both repeats are roughly equal, the first one is a shortcut for
iterating through a list.
This is a record:
set theRecord to {mike: 1, jill: 10, sue: 3, paul: 20}
A record has a key, followed by a colon (:), followed by the value.
Each key-value pair is separated from the next pair by a comma. Values
can be of any type of variable possible in AppleScript
You can have lists of lists, records of lists, lists of records, etc.
Basically you are just storing one set of objects inside another. For
example:
set listList to {{1, 2, 3}, {1, 3, 5}, {2, 4, 6, 8}}
You'll notice that the objects don't even have to be the same type or
of the same length:
set anotherList to {1, {2 , 4, 6}, {3, 5}, "boo"}
In order to get the value of any item in the record you need to get it
explicitly:
set theRecord to {mike: 1, jill: 10, sue: 3, paul: 20}
set theValue to mike of theRecord
--> 1
(1 is the value stored in theValue)
To sum up all the values you need to get each one individually:
set sum to 0
set sum to sum + mike of theRecord
set sum to sum + jill of theRecord
set sum to sum + sue of theRecord
set sum to sum + paul of theRecord
You can also coerce a record into a list. This will lose all of the
keys and will copy only the values into a list:
set theList to theRecord as list
--->{1, 10, 3, 20}
(this is what is stored in theList)
Then you can sum them up:
set theRecord to {mike: 1, jill: 10, sue: 3, paul: 20}
set theList to theRecord as list
set sum to 0
repeat with theValue in theList
set sum to sum + theValue
end repeat
To iterate through a list of lists you would do a nested loop:
set listList to {{1, 2, 3}, {1, 3, 5}, {2, 4, 6, 8}}
set sum to 0
repeat with aList in listList
repeat with theValue in aList
set sum to sum + theValue
end repeat
end repeat
set theList to {server1:{ipaddress:"x.x.x.x",path:"blah"}, server2:{
....
what I want to do is walk the array (repeat with?) and connect to each
server using the list of connection strings:
mount volume smb://domain;uid:password@ipaddress/path
Each connection must be dropped before connecting to the next.
I wondering how I can walk the arrays.
Coming back to your example:
set theStructure to {server1:{ipaddress:"x.x.x.x", path:"foo"},
server2:{ipaddress:"y.y.y.y", path:"bar"}}
This is a record of records. I'm assuming that the actual key of each
server (the names server1, server2, etc.) doesn't matter so we can just
throw it away and create a list of records:
set theList to theStructure as list
-->{{ipaddress:"x.x.x.x", path:"foo"}, {ipaddress:"y.y.y.y",
path:"bar"}}
Now since the interior records are the same between each item we can
iterate through the items and use each record (I included all of the
lines of the example here):
set theStructure to {server1:{ipaddress:"x.x.x.x", path:"foo"},
server2:{ipaddress:"y.y.y.y", path:"bar"}}
set theList to theStructure as list
repeat with aServer in theList
set theAddress to ipaddress of aServer
set thePath to path of aServer
mount volume "smp://domain;uid:password@" & theAddress & thePath
-- do other stuff here
end repeat
And that's the whole tutorial! :-)
- Ken
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.