list.difference and list.unique
Moderators: dorpond, trevor, Azhrei, giliath, Gamerdude, jay, Mr.Ice, lmarkus001
Forum rules
Discussion regarding lmarkus001's framework only. Other posts deleted without notice!
Discussion regarding lmarkus001's framework only. Other posts deleted without notice!
list.difference and list.unique
I am trying to figure out exactly what these two user defined functions actually do.
Re: list.difference and list.unique
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.
Downloads:
- Notepad++ MapTool addon
- RPEdit details (v1.3)
- Coding Tips: Modularity and Design
- Videos: Macro Writing Tools
Re: list.difference and list.unique
Here are the two blocks of code:
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.
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 ]
!!
Re: list.difference and list.unique
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:
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.
Downloads:
- Notepad++ MapTool addon
- RPEdit details (v1.3)
- Coding Tips: Modularity and Design
- Videos: Macro Writing Tools
Re: list.difference and list.unique
OK. Thanks! However, this doesn't help me as I want to combine List A and List B.
Re: list.difference and list.unique
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)]
Downloads:
- Notepad++ MapTool addon
- RPEdit details (v1.3)
- Coding Tips: Modularity and Design
- Videos: Macro Writing Tools
Re: list.difference and list.unique
Thank you! That works great for my purposes.
Re: list.difference and list.unique
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()
GETTING STARTED WITH MAPTOOLS - TUTORIALS, DOCS, VIDEOS, TOOLS, ETC
DISCORD (the new MT forum!)
My stuff
Excel Tools: Table and Light editors
MT Tools: Bag of Tricks: Tools for Maptool, Dungeon Builder I, Dungeon Builder II,onMouseOverEvent.
Frameworks: Dark Heresy, Rogue Trader, Deathwatch, Black Crusade, Only War, SET Card Game, RoboRally
Wiki: Debugging Tutorial, Speed Up Your Macros, Working With Two CODE Levels, Shortcut Keys, Avoiding Stack Overflow, READ THIS
DISCORD (the new MT forum!)
My stuff
Excel Tools: Table and Light editors
MT Tools: Bag of Tricks: Tools for Maptool, Dungeon Builder I, Dungeon Builder II,onMouseOverEvent.
Frameworks: Dark Heresy, Rogue Trader, Deathwatch, Black Crusade, Only War, SET Card Game, RoboRally
Wiki: Debugging Tutorial, Speed Up Your Macros, Working With Two CODE Levels, Shortcut Keys, Avoiding Stack Overflow, READ THIS