compare items of 2 comboboxes - combobox

I want to compare the items of 2 comboboxes.
Lets say combobox1 has the items ("a", "b", "c", "d") and combobox2 has the items ("b", "c").
I want to check the items and remove them from combobox1 if there is equality.
So after comparison combobox1 should have the items ("a", "d") and combobox2 should still have ("b", "c").
How can I reach this behavior?

Try
combobox1.getItems().removeAll(combobox2.getItems());

Related

How can I make an array true or false?

On Code.org, I'm trying to make a quiz that picks an answer based on what the user has placed in the array. However, regardless of what is in the array it always comes out false.
I've tried changing the statement to have more than one equal sign, I've tried doing it backward and making the if statement if list !== [list] and I've tried removing the quotations. Nothing has worked. I've also tried defining the correctlist variable inside the if statement, but that still produces false.
var mylist = ["a". "b", "c"];
var correctlist;
if (mylist == ["a", "b", "c"]) {
correctlist = true;
} else {
correctlist = false;
}
console.log(correctlist);
It always comes out as false.
I expected the console log to state true but it always says false.
You will notice that:["a", "b", "c"] == ["a", "b", "c"]
always returns false.
That is because these are two different arrays. Just because the elements string match, the arrays are not the same.
You will either need to iterate through the elements to compare each, convert them to something that can be compared through a plain equality, or use a library with a "deep" equals.
JSON.stringify(["a", "b", "c"]) == JSON.stringify(["a", "b", "c"])

Swift keep array order whilst filling it

So, I have a 2D array called allBusinesses of type BusinessClass. I fill this array in the following way:
allBusinesses[0].append(contentsOf: [B11, B12, B13, B14, B15, B16])
allBusinesses[1].append(contentsOf: [B21, B22, B23, B24, B25, B26])
Where B11, B12 ... B26 are all BusinessClass instances.
I have another 2D BusinessClass array called myOwnedBusinesses. I create it the following way:
var myOwnedBusinesses: [[BusinessClass]] = [[], []]
In my application, I have a tableView which contains all elements of allBusinesses, where each section contains the rows of the second dimension of the array, so that: allBusinesses[section][row]. When I select a random cell in the tableView, the corresponding BusinessClass element is added to the myOwnedBusinesses array, in the following way:
myOwnedBusinesses[selectedSection].append(allBusinesses[selectedSection][selectedRow])
As you can imagine from seeing the code, if I for instance select the cell at section 0 row 3, then select the cell at section 0 row 2, the order of myOwnedBusinesses will be wrong, being the opposite of what allBusinesses is. As a conclusion, I want to maintain the same order between the two arrays, even though the myOwnedBusinesses array is not always filled.
Here is my solution
let section0 = ["a", "b", "c", "d", "e", "f"]
let section1 = ["h", "i", "j", "k", "l", "m"]
section0.index(of: "b")
var all:[[String]] = [[],[]]
all[0].append(contentsOf: section0)
all[1].append(contentsOf: section1)
To keep the original indexes flatten the original array
let all_flat = all.flatMap {$0}
Let's say the user selects the cells in this order : "d", "e", "a", "h", "m", and "k".
var myArray = [["d", "e", "a"], ["h", "m", "k"]]
And then sort each array inside myArray
myArray = myArray.map {
$0.sorted { str1, str2 in
return all_flat.index(of: str1)! < all_flat.index(of: str2)!
}
}
For your case :
let allBusinesses_flat = allBusinesses.flatMap {$0}
myOwnedBusinesses = myOwnedBusinesses.map {
$0.sorted { b1, b2 in
return all_flat.index(of: b1)! < all_flat.index(of: b2)!
}
}
This solution is expensive, memory wise. Storing the selected indexes would be preferable.

Two Dimensional Array with Excel Range

Goal
I am trying to put data into my spreadsheet using a two dimensional array which can contain one or many products.
Take a look at my two dimensional array:
Now take a look at my wanted result:
I can't seem to correctly place the data into the Excel cells. I thought there was a 1 liner of code that could do this, but I tried a different approach and just can't seem to wrap my head around these 2d arrays... Friday can't come soon enough!
Take a look at my code:
If wSheet IsNot Nothing Then
Dim colRange() As String = {"B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U"}
For i As Integer = products.GetLowerBound(0) To products.GetUpperBound(0)
'Start at B2 ... C2 ... D2 ...
Dim r As Microsoft.Office.Interop.Excel.Range = wSheet.Range(colRange(i) & "2").Resize(products.GetLength(1))
r.Value2 = products
Next
End If
This really just doesn'T work, it will just display the (0,0), (1,0) and (2,0) values to the Excel sheet... Any suggestions?
Bah, I knew it could've been done in a 1 liner! Lol. I swapped the columns / rows in my 2d array and tried to set that as the range.
Then it was just a matter of:
wSheet.Range("B2").Resize(UBound(products, 1), UBound(products, 2)).Value2 = products

Swift 2.1: How do you produce a set from an array?

Scenario:
An array of strings, many are duplicated.
Goal:
Produce a UNIQUE array of strings.
Modus Operandi:
I was thinking of converting the array to a set of strings which become unique; from which to generate a new array of unique strings.
Question: How does one convert a Swift array into a Swift Set?
let nonUniqueArray = ["A", "B", "C", "C", "B", "A"]
let uniqueArray = Array(Set(nonUniqueArray))
print(uniqueArray)
produces
["C", "B", "A"]
Swift 2.2 produces exactly the same result as well.
Have you tried let myset = Set(myarray) ?

F# filter a two-dimensional array with multiple arguments

I've been stuck for a while with this seemingly basic problem. I have a two dimensional array of strings and another one dimensional array of strings. The one dimensional array consists of some of the elements present in one of the columns of the two dimensional array. The result that I wish to get is a two dimensional array which is filtered by the elements in the two-dimensional array. As an example:
two-dimensional array:
[["A", "elephant"], ["B", "dog"] , ["C", "cat"] , ["D", "mouse"], ["E", "giraffe"]]
one-dimensional array:
["elephant" , "cat" , "giraffe"]
desired result:
[["A", "elephant] , ["C", "cat"] , ["E", "giraffe"]]
I thank you in advance for your help. I'm pretty new to F# and trying to learn it has been difficult until now.
cheers
Let's say you have a list of tuples like this one:
let animalList =
[("A", "elephant"); ("B", "dog"); ("C", "cat"); ("D", "mouse"); ("E", "giraffe")]
And another list of animals you want to keep, let's make it a set while we're at it:
let animalsToKeep =
["elephant"; "cat"; "giraffe"] |> Set.ofList
Then define a function that filters a list of tuples, keeping only those that appear in a given set
let filterWithSet set lst =
lst
|> List.filter (fun (_, elem) -> Set.contains elem set)
And call it:
filterWithSet animalsToKeep animalList
The answer depends on what you actually want to do, but it sounds like finding the right representation is the most important part of the question. In your example, your nested lists always contain just two values (e.g. "A" and "elephant") and so it would make more sense to use a list of tuples:
let things = [ ("A", "elephant"); ("B", "dog"); ("C", "cat");
("D", "mouse"); ("E", "giraffe")]
This representation will make things easier, because we only need to check if the second element of the tuple is in the list used for filtering:
let filter = ["elephant" ; "cat" ; "giraffe"]
To do that, you can use List.filter to filter a list. In the condition, you can get the animal using snd (get second element of a tuple) and then use List.exist to see if it is in the list of animals to be included:
things |> List.filter (fun nested ->
let animal = snd nested
filter |> List.exists (fun a -> a = animal))
If you want to make the lookup more efficient, you can create a set of filtered items:
let filter = set ["elephant" ; "cat" ; "giraffe"]
things |> Seq.filter (fun nested -> filter.Contains(snd nested))
And, in fact, you can use function composition to call snd followed by the check:
things |> Seq.filter (snd >> filter.Contains)
This means exactly the same thing as the line above - it takes the tuple with the letter and animal name, extracts the animal name using the snd function and then passes the name to filter.Contains to see if it is in the set.

Resources