AnswerBun.com

How to print data from a few arrays

Stack Overflow Asked by protodimbo on September 25, 2020

I have a few arrays like this but much bigger:

let katakana = ["フシギダネ","フシギソウ","フシギバナ"]
let romadzi = ["Fushigidane","Fushigisou","Fushigibana"]
let number = ["001","002","003"]

And I want to print in one line all data from every array with similar index:

print("Hi im (katakana[0]), or (romadzi[0]) and my nymber is (number[0])")
print("Hi im (katakana[1]), or (romadzi[1]) and my nymber is (number[1])")

How to do that with my huge arrays?

3 Answers

Try

katakana.indices.forEach {
    print("Hi im (katakana[$0]), or (romadzi[$0]) and my nymber is (number[$0])")
}

Given that all arrays are of same size

Correct answer by Sh_Khan on September 25, 2020

   let katakana = ["フシギダネ","フシギソウ","フシギバナ"]
   let romadzi = ["Fushigidane","Fushigisou","Fushigibana"]
   let number = ["001","002","003"]
    
    for i in 0..<katakana.count
    {
        print("Hi im (katakana[i]), or (romadzi[i]) and my nymber is (number[i])")
    }

Answered by Jins George on September 25, 2020

A more efficient and reliable way is to create a custom struct. Each instance contains all information about one person.

The description property provides to get the description string with array[i]

struct Person : CustomStringConvertible {
    let katakana, romadzi, number : String
    
    var description : String {
        return "Hi I'm (katakana), or (romadzi) and my number is (number)"
    }
}

let people = [Person(katakana: "フシギダネ", romadzi: "Fushigidane", number: "001"),
              Person(katakana: "フシギソウ", romadzi: "Fushigisou", number: "002"),
              Person(katakana: "フシギバナ", romadzi: "Fushigibana", number: "003")]


print(people[1]) // Hi I'm フシギソウ, or Fushigisou and my number is 002

and to print all

people.forEach{print($0)}

Answered by vadian on September 25, 2020

Add your own answers!

Related Questions

Animation to play once per session

0  Asked on January 15, 2021 by alejandro-vargas

       

postgres random text in jsonb column

2  Asked on January 15, 2021 by ed1t

         

How to make TestNG print detail message about the failure

2  Asked on January 15, 2021 by gelin-luo

 

Strange igraph behaviour, generating duplicate vertices

1  Asked on January 15, 2021 by simon-mills

   

C++ printing float as nan

1  Asked on January 14, 2021 by acarter

     

Webclient isn’t dowloading everything from URL c#

1  Asked on January 14, 2021 by shynex

       

How to change the entire div content on button click

2  Asked on January 14, 2021 by liel-barouch

   

Sorting and paging nested documents

2  Asked on January 14, 2021 by taras-kohut

 

How to adjust for loop so that it prints list only once?

1  Asked on January 14, 2021 by benito-cano

         

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP