Unable to get values from 2 arrays simultaneously

Hi,

I have 2 arrays

let scores = [2, 2, 8, 6, 1, 2, 1]

var players = ["Bob", "Alice", "James", "Gini", "John", "Alex", "Ray"]

now I just wanted to make a function to get the name and score of players in an array as tuple like
[(Bob, 2), (Alice,2] etc

when I try and make a function like below

func nameAndScores(name: [String], scores: [Int]) -> [(String, Int)]{
    for (names, scores) in players, scores {
        return [(names, scores)]
    }
}

I keep getting errors, the approach works with single array but not with 2 arrays, kindly guide ....

As someone who teaches programming for a living, I would like to strongly recommend you read a proper book first. There is so much wrong with this code, it suggests to me you don't have any prior programming experience, and you're learning as you go, perhaps by watching some YouTube videos and asking questions here as you get stuck?

Without a structured education (even if that's only a book), you will likely get nowhere, and frustrate both yourself and others (who have to take the time to answer your questions).

If are indeed without prior programming experience, I can recommend Swift Apprentice | raywenderlich.com as Apple's book isn't suitable for new learners.

@svanimpe I am extremely sorry for asking such a question, I will try and get more lessons on swift , I was just confused, I have asked only a couple of questions so I am sorry for that as well , I hope you are not going to throw me out of forum as I was just learning and could not solve this problem after many attempts

To directly answer the question, you can do this with the zip function, as in zip(names, scores).

However, you may find it more convenient to define a struct that holds all the information about a player, including name, score, and any other important data.

4 Likes

I can't do that, nor would I. This is a friendly forum ;)

But:

  • Your for-loop is invalid. It looks like you're trying to iterate over a dictionary, or an enumerated array, but the result is just invalid syntax.
  • That loop contains only a return statement, which makes no sense, as it will just return on the first iteration.

This is why I suggest investing in some form of structured education first, as a simple answer like "use a dictionary instead" won't really get you much further.

1 Like

Thats such a mean answer! I'm not surprised @amitsrivastava felt like they needed apologise after getting a response like that. As a teacher you should know that encouragement is the key, not to tell someone who is learning that they are flat out wrong :frowning:

As you say, this is supposed to be a friendly forum, we want to encourage people to use swift, not alienate them.

10 Likes

Just getting your code to work, changing it as little as possible:

let scores = [2, 2, 8, 6, 1, 2, 1]
var players = ["Bob", "Alice", "James", "Gini", "John", "Alex", "Ray"]
func nameAndScores(name: [String], scores: [Int]) -> [(String, Int)] {
    var result: [(String, Int)] = []
    for (names, scores) in zip(players, scores) {
        result.append((names, scores))
    }
    return result
}
let pairs = nameAndScores(name: players, scores: scores)

See For‐In Loops and zip(_:_:).


An even shorter way to write it:

let scores = [2, 2, 8, 6, 1, 2, 1]
var players = ["Bob", "Alice", "James", "Gini", "John", "Alex", "Ray"]
func nameAndScores(name: [String], scores: [Int]) -> [(String, Int)] {
    return Array(zip(name, scores))
}
let pairs = nameAndScores(name: players, scores: scores)

See init(_:).


What you probably want to do instead (unless context you haven’t told us about prevents it):

let scores = [
    "Bob": 2,
    "Alice": 2,
    "James": 8,
    "Gini": 6,
    "John": 1,
    "Alex": 2,
    "Ray": 1
]

See Dictionary.


As @Nevin said, if you ever need to keep track of more than just the score, you should probably use a structure that models the data better:

struct Player {
    let name: String
    var score: Int
    let handicap: Int
}

let playerDefinitions = [
    Player(name: "Bob", score: 2, handicap: 0),
    Player(name: "Alice", score: 2, handicap: 0),
    Player(name: "James", score: 8, handicap: -2),
    Player(name: "Gini", score: 6, handicap: -1),
    Player(name: "John", score: 1, handicap: 1),
    Player(name: "Alex", score: 2, handicap: 0),
    Player(name: "Ray", score: 1, handicap: 1)
]
var players = Dictionary(uniqueKeysWithValues: playerDefinitions.map({ ($0.name, $0) }))

players["John"]?.score += 1

See Structures & Classes.

3 Likes

@svanimpe Please stop promoting your book. Apple's book is suitable for beginners too.

2 Likes

Amit, you don't have to worry about anyone throwing you out of the forum just for asking questions. You're not doing anything abusive.

That said, I agree with Steven that it seems like you're struggling with some basic concepts and might need more structured support than we can reasonably offer as a forum. Contra some of the comments here, I don't think it's unreasonable of him to make that observation, although he seems to have missed that you did mention your previous experience in another thread. I'll take some other things up with him privately.

But as you learn those basic concepts better, whether it's from a book or directly with a teacher, I hope you feel welcome to keep coming back with questions.

8 Likes