list.difference and list.unique

Discussion concerning lmarkus' campaign framework for D&D3.x and Pathfinder.

Moderators: dorpond, trevor, Azhrei, giliath, Gamerdude, jay, Mr.Ice, lmarkus001

Forum rules
Discussion regarding lmarkus001's framework only. Other posts deleted without notice! :)
Post Reply
neofax
Great Wyrm
Posts: 1694
Joined: Tue May 26, 2009 8:51 pm
Location: Philadelphia, PA
Contact:

list.difference and list.unique

Post by neofax »

I am trying to figure out exactly what these two user defined functions actually do.

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: list.difference and list.unique

Post by aliasmask »

You should probably post the code, but if they're anything like json.difference and json.unique then list.difference returns the values not in both lists and list.unique filters out duplicate list items.

neofax
Great Wyrm
Posts: 1694
Joined: Tue May 26, 2009 8:51 pm
Location: Philadelphia, PA
Contact:

Re: list.difference and list.unique

Post by neofax »

Here are the two blocks of code:

Code: Select all

@@ @list.difference
@PROPS@ fontColor=black;autoExecute=true;fontSize=1.00em;sortBy=;color=default;playerEditable=false;applyToSelected=false;group=;tooltip=;minWidth=
<!-- Parameter count checking -->
[ assert( argCount() > 1 && argCount() < 5, "<b>list.difference( longerList, shorterList, delimiter1, delimiter2 ):</b> Requires two list parameters and has optional 3rd and 4th delimiter parameters.", 0 ) ]

<!-- Assign parameters -->
[IF( argCount() == 3 ): delim1 = arg( 2 ); delim1 = "," ]
[IF( argCount() == 4 ): delim2 = arg( 3 ); delim2 = "," ]
[ lgm.list1 = list.unique( arg( 0 ), delim1 ) ]
[ lgm.list2 = list.unique( arg( 1 ), delim2 ) ]

<!-- Variable conditioning -->
[ lgm.l2 = listCount( lgm.list2, delim2 ) ]

<!--  -->
[ FOREACH( el, lgm.list2, delim2 ), CODE:
	{
	[ i = listFind( lgm.list1, el, delim1 ) ]
	[IF( i >= 0 ): lgm.list1 = listDelete( lgm.list1, i, delim1 ) ]
	}]

<!-- Return the final result -->
[ macro.return = lgm.list1 ]

!!

Code: Select all

@@ @list.unique
@PROPS@ fontColor=black;autoExecute=true;fontSize=1.00em;sortBy=;color=default;playerEditable=false;applyToSelected=false;group=;tooltip=;minWidth=
<!-- Parameter count checking -->
[ assert( argCount() > 0 && argCount() < 3, "<b>list.unique( list, delimiter ):</b> Requires one list parameter and has an optional delimiter parameter.", 0 ) ]

<!-- Assign parameters -->
[IF( argCount() == 2 ): delim = arg( 1 ); delim = "," ]
[ lgm.list = arg( 0 ) ]

<!-- Variable conditioning -->
[ lgm.cnt = listCount( lgm.list, delim ) ]
[ lgm.out = "" ]

<!--  -->
[COUNT( lgm.cnt ), CODE:
	{
	[ lgm.val = listGet( lgm.list, roll.count ) ]
	[ lgm.tcount = listContains( lgm.out, lgm.val ) ] 
	[IF( lgm.tcount < 1 ): lgm.out = listAppend( lgm.out, lgm.val, delim ); "" ] 
	}]

<!-- Return the final result -->
[ macro.return = lgm.out ]

!!
The problem is list.difference is not returning the values not in both lists. It is keeping the original list and throwing the second list. I verified this be stepping through the code using the pause debugger.

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: list.difference and list.unique

Post by aliasmask »

Okay, looks like I was wrong. It's not just the doesn't appear in both lists, it's list B subtracted from list A. So, list A = A,B, list B = B,C.. so difference equal A because "B" is subtracted from list A and "C" is ignored because it's not in list A. Basically, list B should be a subset of A and the difference is what is in list A, but not in list B.

Looking at the actual code, I would think it's doing it wrong because it's modifying the original list as it removes items. I would do it this way:

Code: Select all

@@list.difference
[H: list1 = arg(0)]
[H: list2 = arg(1)]
[H, if(argCount > 2): delim1 = arg(2); delim1 = ","]
[H, if(argCount( > 3): delim2 = arg(3); delim2 = ","]
[H, jList1 = json.fromList(list1,delim1)]
[H: jList2 = json.fromList(list2,delim2)]
[H: jDiff = json.difference(jList1,jList2)]
[H: macro.return = json.toList(jDiff,delim1)] 

Code: Select all

@@list.unique
[H: list1 = arg(0)]
[H, if(argCount() > 1): delim1 = arg(1); delim1 = ","]
[H: macro.return = json.toList(json.unique(json.fromList(list1,delim1)),delim1)]
 
Last edited by aliasmask on Mon Aug 22, 2011 10:57 am, edited 1 time in total.

neofax
Great Wyrm
Posts: 1694
Joined: Tue May 26, 2009 8:51 pm
Location: Philadelphia, PA
Contact:

Re: list.difference and list.unique

Post by neofax »

OK. Thanks! However, this doesn't help me as I want to combine List A and List B.

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: list.difference and list.unique

Post by aliasmask »

neofax wrote:OK. Thanks! However, this doesn't help me as I want to combine List A and List B.

Code: Select all

@@list.union
[H: list1 = arg(0)]
[H: list2 = arg(1)]
[H, if(argCount > 2): delim1 = arg(2); delim1 = ","]
[H, if(argCount( > 3): delim2 = arg(3); delim2 = ","]
[H, jList1 = json.fromList(list1,delim1)]
[H: jList2 = json.fromList(list2,delim2)]
[H: jDiff = json.union(jList1,jList2)]
[H: macro.return = json.toList(jDiff,delim1)] 
PS - Should have said that in the original post :)

neofax
Great Wyrm
Posts: 1694
Joined: Tue May 26, 2009 8:51 pm
Location: Philadelphia, PA
Contact:

Re: list.difference and list.unique

Post by neofax »

Thank you! That works great for my purposes.

User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: list.difference and list.unique

Post by wolph42 »

im guessing its not the case, or you wouldn't go through so much trouble, but if the lists both have unique entries (so no doubles) you can just use Wiki: listAppend()

Post Reply

Return to “D&D 3.5/Pathfinder 1e Campaign Macros”