List extraction and creation help required

Hello all,
I’m going to ask what is probably a very simple thing, but my mind isn’t working too well today, and thus I cannot work out what I should be doing.

OS is 10.5.4, machine is Intel iMac.

Here is what I want to do:

I have the following list:

set mastergrouplist to {{"markwarren", {"group one", "group two", "group three"}}, {"carlwarren", {"group one", "group two", "group four"}}}

What I need to do is to is convert that list into the following list:

{{"group one",{"markwarren","carlwarren"}},{"group two",{"markwarren","carlwarren"}},{"group three",{"markwarren"}},{"group four",{"carlwarren"}}}

Ideally I want to avoid using any third party scripting additions, but using ‘do shell script’ to leverage shell functionality would be ok though. Any advice would be greatly appreciated.

Hi,

you’re wrong, this is NOT A SIMPLE THING :wink:

probably there is a smarter solution, but this should work


set mastergrouplist to {{"markwarren", {"group one", "group two", "group three"}}, {"carlwarren", {"group one", "group two", "group four"}}}

set {newGroupList, newNameList} to {{}, {}}
repeat with i from 1 to (count mastergrouplist)
	set groupList to item 2 of item i of mastergrouplist
	repeat with j from 1 to (count groupList)
		if item j of groupList is in newGroupList then
			repeat with k from 1 to (count newGroupList)
				if item k of newGroupList is item j of groupList then exit repeat
			end repeat
			set end of item k of newNameList to item 1 of item i of mastergrouplist
		else
			set end of newGroupList to item j of groupList
			set end of newNameList to {item 1 of item i of mastergrouplist}
		end if
	end repeat
end repeat
set newMastergrouplist to {}
repeat with i from 1 to count newNameList
	set end of newMastergrouplist to {item i of newGroupList, item i of newNameList}
end repeat
newMastergrouplist

What an intriguing problem! Kudos to Stefan for producing a working solution in such a short time. :cool:

Here’s a “smart” way, though I shouldn’t think it’s any better.

set mastergrouplist to {{"markwarren", {"group one", "group two", "group three"}}, {"carlwarren", {"group one", "group two", "group four"}}}

set recordScrptTxt to "set rec to "
set actionScrptTxt to ""
repeat with thisPerson in mastergrouplist
	set thisName to beginning of thisPerson
	repeat with thisGroup in end of thisPerson
		set recordScrptTxt to recordScrptTxt & "{|" & thisGroup & "|:{\"" & thisGroup & "\", {}}} & "
		set actionScrptTxt to actionScrptTxt & "set end of end of |" & thisGroup & "| of rec to \"" & thisName & "\"" & return
	end repeat
end repeat
set newMastergrouplist to (run script (text 1 thru -4 of recordScrptTxt & return & actionScrptTxt & "return (rec as list)"))

Thanks for the replies! Good to know that it wasn’t a simple thing to do. Code is much appreciated!

The idea behind all of this was to take a file in the format of:

username(tab)password(tab)groupone(tab)grouptwo(tab)…groupfive(return)

where password and the group listings are optional, and to create two files, one with user information, the other with the group assignments. The files would be of a format that allows them to be imported into workgroup manager. There are probably already scripts to do this, but I wanted to try it myself first.

Once again, thanks for all your help