i have a array of tuple like
var contactsname = [(String,String)]()//firstname,lastname
example = [(alex,joe),(catty,drling),(alex,fox),(asta,alex)]
i need to search elements which are matching the firstname or lastname and return those all elements matching the key
func searchElementsForkey(key:String)->[(string,String)]{ //code here }
searchElementsForKey("alex") = [(alex,joe),(alex,fox),(asta,alex)]
You can go like this.
var contactsname = [(String,String)]()//firstname,lastname
contactsname = [("alex","joe"),("catty","drling"),("alex","fox"),("asta","alex")]
let key = "alex"
If you want to exact match the search name either with First name or Last name
let filterArray = contactsname.filter { $0.0 == key || $0.1 == key }
If you want to check First name and Last name contains specific string for that you can use contains
let filterArray = contactsname.filter { $0.0.contains(key) || $0.1.contains(key) }
Related
I am a stuck on this piece of code. I have an Example sheet with two tabs. The first tab is new items. A new item is comprised of two pieces, a attribute code (string), and an item ID (number). In the other tab "Locations" there are a bunch of empty locations. Each location has primary attribute (string), and a set of secondary attribute codes in a longer string.
I have assigned these two ranges to two unique arrays.
function matchcodes() {
var locss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Locations');
var lastlocRow = locss.getLastRow();
var newitems = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('New Items');
var lastNIRow = newitems.getLastRow();
var itemcodes = newitems.getRange("A1:B" + lastNIRow).getValues();
var locations = locationssheet.getRange("A2:D" + lastlocationRow).getValues();
Logger.log(itemcodes)
Logger.log(locations)}
What I am attempting to do is compare itemcodes[i][0] to locations[j][2] (match item attribute with location primary attribute). If the strings match I want to copy itemcodes[i][1] (ItemID) and set it as the value of locations[j][1]. If the strings do not match check the next iteration of locations[j][2].
If no matching attributes are found in locations[j][2], I would like to see if it is contained as a substring in locations[j][3] (starting back at the top and iterating through the whole list of secondary attributes. If the substring code is contained in loactions[j][3] I would like take the same action in the first IF condition.
Once a new item is matched, the loop can break, and the next item can be located itemcodes[i+1][0]. If no match is found in the primary or secondary search, also iterate to the next new item.
Where I'm struggling is writing the condition statements to compare both strings and substrings within strings.
//for (var i = 0; i < itemcodes.length; i++) {
//for (var j = 0; j < locations.length; j++) {
//if (itemcodes[i][0] == locations[j][2]) {
// I want set the value of locations[j][1] with itemcodes[i][1]
}
// if no match is found in entire [j][2] column, search for substring in locations[j][3] column
//if item match is found, or no match is found in all of [j][2] or [j][3] break loop and iterate to [i+1][0] and start the next loop
Input (3 iterations)
Results
Any help would be much appreciated. Or if you can point me to a similar thread. (I've not had any success finding a similar example) Thanks in advance!
Try:
function matchCodes() {
const newItems = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(`New Items`)
const locations = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(`Locations`)
const newItemsValues = newItems.getDataRange().getValues()
const locationsValues = locations.getDataRange().offset(1, 0).getValues()
newItemsValues.forEach(([attribute, id]) => {
const primaryTarget = locationsValues.findIndex(row => row[2] === attribute && row[1] === ``)
if (primaryTarget !== -1) return locationsValues[primaryTarget][1] = id
const secondaryTarget = locationsValues.findIndex(row => row[3].includes(attribute) && row[1] === ``)
if (secondaryTarget !== -1) return locationsValues[secondaryTarget][1] = id
})
locations.getDataRange().offset(1, 0).setValues(locationsValues)
}
Learn More:
Array.findIndex()
Destructuring Assignment
Not sure if I need reduce, sorting, filtering or using the newer unique methods. I've even tried Equatable solutions.
I need to auto-identify keys that have matching values and take only those keys and put them into a new dictionary or array.
var userDB: [String:Any] = ["userID1":"gameIDa", "userID2":"gameIDa", "userID3":"gameIDc", "userID4":"gameIDd", "userID5":"gameIDe"]
As you can see only these two IDs have the same value of gameIDa. I need output of this result.
// [userID1, userID2]
Thanks
You can use Dictionary(grouping:by:) to achieve this easily and then reduce it to a dictionary which contains only entries with multiple values.
var userDB: [String: String] = ["userID1":"gameIDa", "userID2":"gameIDb", "userID3":"gameIDc", "userID4":"gameIDa", "userID5":"gameIDe"]
let dict = Dictionary(grouping: userDB.keys) { userDB[$0] ?? "" }.reduce(into: [String:[String]](), { (result, element) in
if element.value.count > 1 {
result[element.key] = element.value
}
})
dict
["gameIDa": ["userID1", "userID4"]]
Firstly, in order to be able to compare values, the Dictionary Value type needs to be an Equatable one. Once this is fulfilled, you can easily filter the keys that hold the queried value:
extension Dictionary where Value: Equatable {
func keysWithCommonValues() -> [Key] {
// go over all keys and keep ones for which the dictionary has one another key with the same value
return keys.filter { key in contains { $0.key != key && $0.value == self[key] } }
}
}
// userDB is not inferred as [String:String]
var userDB = ["userID1":"gameIDa", "userID2":"gameIDa", "userID3":"gameIDc", "userID4":"gameIDd", "userID5":"gameIDe"]
print(userDB.keysWithCommonValues()) // ["userID1", "userID2"]
I have string array in Swift 2:
var myList : [String] = []
And I have dynamic strings inside and I explode them with * character myList examples:
print(myList[0]) output = 2018-04-05*type2*namea
print(myList[1]) output = 2018-04-05*type2*nameb
print(myList[2]) output = 2018-04-05*type3*nameb
print(myList[3]) output = 2018-04-06*type3*named
I want to delete objects have type3 ones in myList:
IF IN same date AND same name AND have type2 ones
Must be my strings like that:
print(myList[0]) output = 2018-04-05*type2*namea
print(myList[1]) output = 2018-04-05*type2*nameb
print(myList[2]) output = 2018-04-06*type3*named
This item below must be deleted:
print(myList[2]) output = 2018-04-05*type3*nameb
I want to delete type3 ones in myList if before have type2 with same date and same name basically.
Explain:
2018-04-05*type2*nameb and 2018-04-05*type3*nameb, have same date and same name but 2018-04-05*type3*nameb before have type2(2018-04-05*type2*nameb) ? so 2018-04-05*type3*nameb line must be delete
How can I do it?
This playground code will do what you want:
//: Playground - noun: a place where people can play
import UIKit
let myList = ["2018-04-05*type2*namea",
"2018-04-05*type2*nameb",
"2018-04-05*type3*nameb",
"2018-04-06*type3*named"]
//Define a class that lets us map from a string to a date, type, and name string
class ListEntry {
let fullString: String
//define lazy vars for all the substrings
lazy var subStrings: [Substring] = fullString.split(separator: "*")
lazy var dateString = subStrings[0]
lazy var typeString = subStrings[1]
lazy var nameString = subStrings[2]
//Create a failable initializer that takes a full string as input
//and tries to break it into exactly 3 substrings
//using the "*" sparator
init?(fullString: String) {
self.fullString = fullString
if subStrings.count != 3 { return nil }
}
}
print("---Input:---")
myList.forEach { print($0) }
print("------------")
//Map our array of strings to an array of ListEntry objects
let items = myList.compactMap { ListEntry(fullString: $0) }
//Create an output array
var output: [String] = []
//Loop through each item in the array of ListEntry objects, getting an index for each
for (index,item) in items.enumerated() {
//If this is the first item, or it dosn't have type == "type3", add it to the output
guard index > 0,
item.typeString == "type3" else {
print("Adding item", item.fullString)
output.append(item.fullString)
continue
}
let previous = items[index-1]
/*
Add this item if
-the previous type isn't "type2"
-the previous item's date doesn't match this one
-the previous item's name doesn't match this one
*/
guard previous.typeString == "type2",
item.dateString == previous.dateString,
item.nameString == previous.nameString else {
print("Adding item", item.fullString)
output.append(item.fullString)
continue
}
print("Skipping item ", item.fullString)
}
print("\n---Output:---")
output.forEach { print($0) }
The output of the code above is:
---Input:---
2018-04-05*type2*namea
2018-04-05*type2*nameb
2018-04-05*type3*nameb
2018-04-06*type3*named
------------
Adding item 2018-04-05*type2*namea
Adding item 2018-04-05*type2*nameb
Skipping item 2018-04-05*type3*nameb
Adding item 2018-04-06*type3*named
---Output:---
2018-04-05*type2*namea
2018-04-05*type2*nameb
2018-04-06*type3*named
I'll start you off with a simple (albeit hack-ish) approach:
let myList = ["2018-04-05*type2*namea", "2018-04-05*type2*nameb", "2018-04-05*type3*nameb", "2018-04-06*type3*named"]
Define the function:
func swapLastTwoComps(_ s: String) -> String {
let parts = s.split(separator: "*")
return [parts[0], parts[2], parts[1]].joined(separator: "*")
}
Now if you do
let myListS = myList.map {swapLastTwoComps($0)}.sorted()
you get
["2018-04-05*namea*type2", "2018-04-05*nameb*type2", "2018-04-05*nameb*type3", "2018-04-06*named*type3"]
i.e. the sort has left strings to be removed adjacent and to the right of their equivalent, so now you can easily loop through the array and remove the strings you want (because you only need to compare each String's prefix with the String immediately to its left to determine whether it should be removed).
Once you've done that, map swapLastTwoComps over the final array again to restore the strings to their previous format.
I would to know how to get key if I have the values. Which class get higher marks?
let higherMarks = [
"ClassA": [10,20,30,40,50,60],
"ClassB": [15,25,35,45,55,65],
"ClassC": [18,28,38,48,58,68],
]
var largest = 0
var className = ""
for (classTypes, marks) in higherMarks {
for mark in marks {
if mark > largest {
largest = mark
}
}
}
print(largest)
What I'm saying in my comment is that you need to get the classTypes when you get the mark. Because when you get the higher mark, you want to also get the corresponding key value.
Keeping your code's logic I would do something like this:
let higherMarks = [
"ClassA": [10,20,30,40,50,60],
"ClassB": [15,25,35,45,55,65],
"ClassC": [18,28,38,48,58,68],
]
func findBestClass(in results: [String: [Int]]) -> (name: String, score: Int) {
var largest = 0
var type = ""
for (classType, marks) in results {
if let max = marks.max(), max > largest {
largest = max
type = classType
}
}
return (type, largest)
}
let best = findBestClass(in: higherMarks)
print("The best class is \(best.name) with a score of \(best.score).")
I just replaced your inner loop with .max() and changed the name of the key variable because it should not be plural. My method also returns a tuple because I find it relevant in this situation. But I didn't change your logic, so you can see what I meant by "also get the classTypes".
i have a struct array that i want "break up" into smaller arrays that can be called as needed or at least figure out how i can map the items needed off one text value.
the struct:
struct CollectionStruct {
var name : String
var description : String
var title : String
var image : PFFile
var id: String
}
and the array made from the struct
var collectionArray = [CollectionStruct]()
var i = 0
for item in collectionArray {
print(collectionArray[i].name)
i += 1
}
printing partArray[i].name gives the following result:
pk00_pt01
pk00_pt02
pk00_pt03
pk01_pt01
pk01_pt02
pk01_pt03
pk01_pt04
pk01_pt05
pk01_pt06
pk01_pt07
pk01_pt08
this is just some test values but there could be thousands of entries here so i wanted to filter the entire array just by the first 4 characters of [i].name i can achieve this by looping through as above but is this achievable using something like .map?
I wanted to filter the entire array just by the first 4 characters of
[i].name
You can achieve this by filtering the array based on the substring value of the name, as follows:
let filteredArray = collectionArray.filter {
$0.name.substring(to: $0.name.index($0.name.startIndex, offsetBy: 4)).lowercased() == "pk00"
// or instead of "pk00", add the first 4 characters you want to compare
}
filteredArray will be filled based on what is the compared string.
Hope this helped.
If you want to group all data automatically by their name prefix. You could use a reducer to generate a dictionary of grouped items. Something like this:
let groupedData = array.reduce([String: [String]]()) { (dictionary, myStruct) in
let grouper = myStruct.name.substring(to: myStruct.name.index(myStruct.name.startIndex, offsetBy: 4))
var newDictionart = dictionary
if let collectionStructs = newDictionart[grouper] {
newDictionart[grouper] = collectionStructs + [myStruct.name]
} else {
newDictionart[grouper] = [myStruct.name]
}
return newDictionart
}
This will produce a dictionary like this:
[
"pk00": ["pk00_pt01", "pk00_pt02", "pk00_pt03"],
"pk01": ["pk01_pt01", "pk01_pt02", "pk01_pt03", "pk01_pt04", "pk01_pt05", "pk01_pt06", "pk01_pt07"],
"pk02": ["pk02_pt08"]
]
Not sure if i am understanding you correctly but it sounds like you are looking for this...
To create a new array named partArray from an already existing array named collectionArray (that is of type CollectionStruct) you would do...
var partArray = collectionArray.map{$0.name}