#How to split a list into two separate lists, based on a given list

1 messages · Page 1 of 1 (latest)

frail flint
#

I'm trying to split a list of options into two separate lists based on a given property list
In this instance,

  1. The known list of Simple Variations is defined as SimpleV
  2. The total list of Variations is defined as options
  3. Without the If within the While section the macro runs as expected but does not add the StrProp because those aren't set
[h: SimpleV = "Covert, Delayed, Distracting, Extended, Mine, Precision, Shaped, Torpedo, Demolition, Quick"]
[h: options = "Blinding,Deafening,Disgusting,Flashbang,Frightening,Shrapnel,Whelming,Befuddling,Clearing,Disjunction,Lacerating,Rubberized,Stunning,Covert,Delayed,Distracting,Extended,Mine,Precision,Shaped,Torpedo,Demolition,Quick"]
[h: Variations = ""]
[h, count(Known,""), code:{    
    [h: setup = input(    "Variation|"+options+"|Known Variations|List|VALUE=STRING"        
    )]
    [h: abort(setup)]
    [h: options=listDelete(options,listFind(options,Variation))]
    [h: Variations=listAppend(Variations,Variation)]
}]

[t, while(listCount(Variations)>0),code:{
    [t, if(listFind(SimpleV,listGet(Variations,0))==-1),code:{
        [t: Complex=listAppend(Complex,listGet(Variations,0))]
    }:{
        [t: Simple=listAppend(Simple,listGet(Variations,0))]
    }]
    [h: Variations= listDelete(Variations,0)]
}]
[h:PowersCont = setStrProp(PowersCont,"Simple",Simple)]
[h:PowersCont = setStrProp(PowersCont,"Complex",Complex)]
alpine pelican
#

You have a long list options. You want to remove everything that is also in the short list SimpleV ?
What's with the input? You also remove something the user selects?

frail flint
#

For the input, I'm wanting to ensure they don't select the same item twice as it would be redundant

#

I haven't look too deep into JSON yet and that may resolve a majority of my issues.

alpine pelican
#

You could do an input window with checkboxes and tell the user to select X boxes. You can then make sure they actually picked the correct amout, but you don't have to prompt with several windows if the user does not mess up.

frail flint
#

I think my hesitancy with the checkboxes is the sheer number of items, but I guess with that I can better seperate the complex, simple, and greater variations
I was aiming for this to be a one and done setup

alpine pelican
#

I don't know the exact use and rules, so you have to make that call. But my first impulse would be to rather show the user the actual full choice and essentially make clear right away that X things need to be picked, instead of having several windows pop up after one another. Manage expectations right away.

frail flint
#

rooThink alright so that would be 23 check boxes alright, I'll work on that thanks

alpine pelican
#

before you hardcode them let me look up some of my old code

#

you can essentially just number the checkboxes internally instead of giving everything explicit names by hand

#
[h: allVariations = json.fromList("Blinding,Deafening,Disgusting,Flashbang,Frightening,Shrapnel,Whelming,Befuddling,Clearing,Disjunction,Lacerating,Rubberized,Stunning,Covert,Delayed,Distracting,Extended,Mine,Precision,Shaped,Torpedo,Demolition,Quick")]

[h: inputQuery = 
strformat("label|<html><span style='font-size:12pt;'>HEADER TEXT HERE</span></html>|<html><span></span></html>|LABEL|SPAN=true")]

[h, foreach(variation, allVariations), code:
  {    [h: inputQuery = json.append(inputQuery, strformat("check%{roll.count}|0|<html>%{variation}</html></html>|CHECK"))] }
]
[h: checkCounter = roll.count]

[h: abort(input(json.toList(inputQuery, "##")))]

[h: checkedVariations = ""]
[h, for(idx, 0, checkCounter), if(eval("check"+idx) == 1):
    checkedVariations = json.append(checkedVariations, json.get(allVariations, idx))
]

[r: checkedVariations]
#

Basic version that you should be able to use if you want.
I just print the selected ones at the moment. You can use the json functions linked above to get the portions that you need.

#

Please ask if anything is unclear.

frail flint
#

rooThink I'm gonna need to look into lines 3 onward to make sure I understand the logic personally 😂 I did this quick and dirty and it seems to have worked, but gonna probably suffer trying to pull it from StrProp oh wait it should retain it's nature as a json array because of it's structure

[h: SimpleV = json.fromList("Covert, Delayed, Distracting, Extended, Mine, Precision, Shaped, Torpedo, Demolition, Quick")]
[h: options = "Blinding,Deafening,Disgusting,Flashbang,Frightening,Shrapnel,Whelming,Befuddling,Clearing,Disjunction,Lacerating,Rubberized,Stunning,Covert,Delayed,Distracting,Extended,Mine,Precision,Shaped,Torpedo,Demolition,Quick"]
[h: Variations = ""]
[h, count(Known,""), code:{    
    [h: setup = input(    "Variation|"+options+"|Known Variations|List|VALUE=STRING"        
    )]
    [h: abort(setup)]
    [h: options=listDelete(options,listFind(options,Variation))]
    [h: Variations=listAppend(Variations,Variation)]
}]
[h: Variations =json.fromList(Variations)]
[h: Simple = json.intersection(SimpleV,Variations)]
[h: Complex = json.difference(Variations,SimpleV)]

[h:PowersCont = setStrProp(PowersCont,"Simple",Simple)]
[h:PowersCont = setStrProp(PowersCont,"Complex",Complex)]```
#

Thanks for your help!

alpine pelican
#

You're welcome.
Would you like to use the checkboxes at some point? In that case I could explain what my code does. If you rather want to stick with your current version I will simply leave it as it is now.

#

btw: You can convert the json back to lists if thats easier within the StrProp.

#

I just saw your edit

frail flint
#

Maybe checkboxes would be better eventually, and I'd be curious if I could group the checkboxes into 4 sectors (complex, simple, greater complex, greater simple)

#

but that can be a different day 😂

alpine pelican
#

@frail flint
Extended version with grouped entries.

[h: variationGroups = json.set("",
    "Complex", json.fromList("Blinding,Deafening,Disgusting,Flashbang,Frightening,Shrapnel,Whelming"),
    "Greater Complex", json.fromList("Befuddling,Clearing,Disjunction,Lacerating,Rubberized,Stunning"),
    "Simple", json.fromList("Covert,Delayed,Distracting,Extended,Mine,Precision,Shaped,Torpedo"),
    "Greater Simple", json.fromList("Demolition,Quick")
    )
]
[h: allVariations = ""]

[h: inputQuery = strformat("label|<html><span style='font-size:12pt;'>HEADER TEXT HERE</span></html>|<html><span></span></html>|LABEL|SPAN=true")]

[h: checkCounter = 0]
[h, foreach(variationGroupName, variationGroups), code:
  { [h: inputQuery = json.append(inputQuery, strformat("label|<html><span style='font-size:12pt;'>--- %{variationGroupName} ---</span></html>|<html><span></span></html>|LABEL|SPAN=true"))]
    [h: variationGroupEntries = json.get(variationGroups, variationGroupName)]
    [h: allVariations = json.merge(allVariations, variationGroupEntries)]
    
    [h, foreach(variation, variationGroupEntries):
        inputQuery = json.append(inputQuery, strformat("check%s|0|<html>%{variation}</html></html>|CHECK", roll.count + checkCounter))
    ]
    [h: checkCounter = checkCounter + json.length(variationGroupEntries)] 
  }
]

[h: abort(input(json.toList(inputQuery, "##")))]

[h: checkedVariations = ""]
[h, for(idx, 0, checkCounter), if(eval("check"+idx) == 1):
    checkedVariations = json.append(checkedVariations, json.get(allVariations, idx))
]

[r: checkedVariations]
frail flint
#

Thanks, I’ll try to deconstruct it so I can make sure I understand it

alpine pelican
#

I'll type up an explanation

alpine pelican
#

set up the variations in a json object. the key is the group name and the corresponding value is a json array of the variations.

[h: variationGroups = json.set("",
    "Complex", json.fromList("Blinding,Deafening,Disgusting,Flashbang,Frightening,Shrapnel,Whelming"),
    "Greater Complex", json.fromList("Befuddling,Clearing,Disjunction,Lacerating,Rubberized,Stunning"),
    "Simple", json.fromList("Covert,Delayed,Distracting,Extended,Mine,Precision,Shaped,Torpedo"),
    "Greater Simple", json.fromList("Demolition,Quick")
    )
]

set up a variable to collect all variations. I also want a non-nested version at the end to read the checkbox index.

[h: allVariations = ""]

inputQuery will be handed to the input() function eventually. It collects entries. This is the pure text label at the top.

[h: inputQuery = strformat("label|<html><span style='font-size:12pt;'>HEADER TEXT HERE</span></html>|<html><span></span></html>|LABEL|SPAN=true")]

running total for used checkboxes

[h: checkCounter = 0]
#

Now two loops.
Basic idea: For each group we want the group name (key) as a label entry. We then want the entries (value) to each receive a checkbox.

Outer loop: Get the group names. Handle the label. Then give the entries to another loop.

[h, foreach(variationGroupName, variationGroups), code:
  { [h: inputQuery = json.append(inputQuery, strformat("label|<html><span style='font-size:12pt;'>--- %{variationGroupName} ---</span></html>|<html><span></span></html>|LABEL|SPAN=true"))]
    [h: variationGroupEntries = json.get(variationGroups, variationGroupName)]
    [h: allVariations = json.merge(allVariations, variationGroupEntries)]

Inner loop: Each variation gets its own input entry with checkbox: "check??|0|variation|CHECK". The first part check?? is a numbered variable, the number is roll.count + checkCounter. We end up with check0, check1, ... up to check22.

    [h, foreach(variation, variationGroupEntries):
        inputQuery = json.append(inputQuery, strformat("check%s|0|<html>%{variation}</html></html>|CHECK", roll.count + checkCounter))
    ]
    [h: checkCounter = checkCounter + json.length(variationGroupEntries)] 
  }
]

Call up the input window. We give it the collected inputQuery and format it in a way the input() function understands (from json to a list with "##" as separators. The input() function understands those as separators for entries.)

[h: abort(input(json.toList(inputQuery, "##")))]

We now need to find out which checkboxes were clicked. We need to read the variables check0 to check22. For that we can use eval("check"+number).

[h: checkedVariations = ""]
[h, for(idx, 0, checkCounter), if(eval("check"+idx) == 1):
    checkedVariations = json.append(checkedVariations, json.get(allVariations, idx))
]

[r: checkedVariations]
tawdry mica
#

Is this list to be saved and modified, ie, you later need to add or remove an option?

frail flint
#

Not really. It’s a once per level selection/swap ability I don’t foresee adding/removing the variations. But I will be saving the output to a strProp/list on the token itself to read it elsewhere

tawdry mica
#

So, once a player selects his options, they never change? (I'm not talking about the master list, just the selections)

frail flint
#

Right, the player’s selections won’t change unless they level up. In which case they can swap one of the selections.

tawdry mica
#

You also want to validate they have the right number of checked options too, right? Is there a restriction to the groups they choose?

#

Known is Artificer(level?)/3, so, round up or minimum of 1? if Artificer changes so Known goes up, they can add another option? Let's say Artificer is 3, they pick 1 options, Artificer is 4, it lists they're existing option and allows 1 change (uncheck and check new option), if Artificer is 6 (from 5) then they can add a new option and change the old one. Is this right?

frail flint
# tawdry mica Known is Artificer(level?)/3, so, round up or minimum of 1? if `Artificer` chang...

largely yes, I'm not too concerned about tracking which options they have selected here, because it will be on their sheets. This is so I can make a macro call when the variations actually apply. Both screenshots are using the same Concussive Boom macro, but one is called as part of the regular attack action the other is when I specifically attack with the cantrip equivalent

        In addition to the attack’s normal effects, it produces a wave of force to those within the splash area of the attack. All creatures within the splash radius of the attack must make a Strength saving throw against your spell save DC or be knocked prone.```