Reputation: 141
I have the array below:
let dict = [("Steve", 17),
("Marc", 38),
("Xavier", 21),
("Rolf", 45),
("Peter", 67),
("Nassim", 87),
("Raj", 266),
("Paul", 220),
("Bill", 392)]
How to loop through it and switch into the index in order to make operations in the three first, the three next and the last three.
Upvotes: 0
Views: 1237
Reputation: 163
import Foundation
let dict = [("Steve", 17),
("Marc", 38),
("Xavier", 21),
("Rolf", 45),
("Peter", 67),
("Nassim", 87),
("Raj", 266),
("Paul", 220),
("Bill", 392)]
for (index, (name, value)) in dict.enumerated() {
switch index {
case 0...2:
// Operations for the first three elements
print("\(name) is in the first group with value \(value)")
case 3...5:
// Operations for the next three elements
print("\(name) is in the second group with value \(value)")
case 6...8:
// Operations for the last three elements
print("\(name) is in the last group with value \(value)")
default:
// Handle any additional cases here
break
}
}
Upvotes: 0
Reputation: 716
You could split your array by chunks of given size using Array
extension:
extension Array {
func chunked(by distance: IndexDistance) -> [[Element]] {
return stride(from: startIndex, to: endIndex, by: distance).map {
let newIndex = index($0, offsetBy: distance, limitedBy: endIndex) ?? endIndex
return Array(self[$0 ..< newIndex])
}
}
}
And then use it like:
let splittedArrays = dict.chunked(by: 3)
splittedArrays.enumerated().forEach { index, subArray in
switch index {
case 0:
//Do needed opertaions for the first group
print("Players: \(subArray.map{$0.0}.joined(separator: ", "))")
case 1, splittedArrays.endIndex - 1:
//Do needed opertaions for the second and last group
print("Result: \(subArray.map{$0.1}.reduce(0, +))")
default:
break
}
}
Output:
Players: Steve, Marc, Xavier
Result: 199
Result: 878
Upvotes: 0
Reputation: 154583
You can use dict.enumerated()
to get a sequence of (idx, item)
tuples that for
can then enumerate through. For example:
let dict = [("Steve", 17),
("Marc", 38),
("Xavier", 21),
("Rolf", 45),
("Peter", 67),
("Nassim", 87),
("Raj", 266),
("Paul", 220),
("Bill", 392)]
for (idx, item) in dict.enumerated() {
let (name, value) = item
switch (idx / 3) {
case 0:
print("\(name) is in the first group")
case 1:
print("\(name) is in the second group")
case 2:
print("\(name) is in the third group")
default:
print("\(name) not in first 3 groups")
}
print("value is \(value)")
}
Output:
Steve is in the first group value is 17 Marc is in the first group value is 38 Xavier is in the first group value is 21 Rolf is in the second group value is 45 Peter is in the second group value is 67 Nassim is in the second group value is 87 Raj is in the third group value is 266 Paul is in the third group value is 220 Bill is in the third group value is 392
or equivalently, instead of doing integer math based upon the index, you could just switch on it directly but use a range for the cases:
switch idx {
case 0...2:
print("\(name) is in the first group")
case 3...5:
print("\(name) is in the second group")
case 6...8:
print("\(name) is in the third group")
default:
print("\(name) not in first 3 groups")
}
Upvotes: 1
Reputation: 6018
You can specify step of index
by where clause:
for index in 0..<dict.count where index % 3 == 0 {
// here you can do action with index, index + 1, index + 2 items in way you need
}
Upvotes: 0