Making a list of records

Hi all,

I’m attempting to make a list of records, and I’m failing at some point. The code below will be turned into a loop so that I’m not repeating myself. However, even with this structure I’m unable to add a new record to the list. Can it even be done?

	--Place each row into a record
	set actionRecord to {actionID:"1", actionDescription:"Description", actionStarted:"15/09/1980", actionFinished:"15/09/2000", ownerEmail:"foo@bar.com"}
	
	--Read each actionRecord into recordList
	set recordList to recordList & actionRecord
	
	--Place each row into a record
	set actionRecord to {actionID:"2", actionDescription:"Description2", actionStarted:"15/09/1980", actionFinished:"15/09/2000", ownerEmail:"foo@bar.com"}
	
	--Read each actionRecord into recordList
	set recordList to recordList & actionRecord

This apparently only adds the first record’s 5 items to the recordList. When it gets to the second record the recordList is not added to or overwritten…

Any ideas?

Hello.

A) You should initialize your recordlist:

set recordList to {}

B.) This is the correct way of appending a record or an item in general to a list:

set end of recordList to actionRecord

I hope this helps

McUserII’s shown how to do it.

If you want the technical explanation for what you were seeing, you were concatenating records to records:

-- When a record is concatenated to empty braces, the braces are taken to represent an empty record and the result too is a record.
set recordList to {}
set actionRecord to {actionID:"1", actionDescription:"Description", actionStarted:"15/09/1980", actionFinished:"15/09/2000", ownerEmail:"foo@bar.com"}
set recordList to recordList & actionRecord
--> {actionID:"1", actionDescription:"Description", actionStarted:"15/09/1980", actionFinished:"15/09/2000", ownerEmail:"foo@bar.com"}


-- When two records are concatenated, the result contains the properties from both records. Where the same properties occurs in both records, their values in the result are those from the first (left) record. 
set actionRecord to {actionID:"2", actionDescription:"Description2", actionStarted:"15/09/1980", actionFinished:"15/09/2000", ownerEmail:"foo@bar.com"}
set recordList to recordList & actionRecord
--> {actionID:"1", actionDescription:"Description", actionStarted:"15/09/1980", actionFinished:"15/09/2000", ownerEmail:"foo@bar.com"}

If for some reason you have to build a list of records by concatenation, the records must be presented in their own lists, so that you’re concatenating lists to lists:

-- When a list is concatentated to empty braces, they braces are taken to represent an empty list and the result too is a list.
set recordList to {}
set actionRecord to {actionID:"1", actionDescription:"Description", actionStarted:"15/09/1980", actionFinished:"15/09/2000", ownerEmail:"foo@bar.com"}
set recordList to recordList & {actionRecord} -- NB. actionRecord in a list.
--> {{actionID:"1", actionDescription:"Description", actionStarted:"15/09/1980", actionFinished:"15/09/2000", ownerEmail:"foo@bar.com"}}

-- When two lists are concatenated, the result contains the items from both lists. 
set actionRecord to {actionID:"2", actionDescription:"Description2", actionStarted:"15/09/1980", actionFinished:"15/09/2000", ownerEmail:"foo@bar.com"}
set recordList to recordList & {actionRecord} -- Ditto.
--> {{actionID:"1", actionDescription:"Description", actionStarted:"15/09/1980", actionFinished:"15/09/2000", ownerEmail:"foo@bar.com"}, {actionID:"2", actionDescription:"Description2", actionStarted:"15/09/1980", actionFinished:"15/09/2000", ownerEmail:"foo@bar.com"}}

But the ‘set end of list’ method McUsrII’s given is better, being more efficient and less confusing! :slight_smile:

Hi Nigel,

Many thanks for the technical explanation. I completely forgot that “&” is a concatenator.

Cheers.

Hey McUsrII,

Perfect. That did it.