VGDev
VGDev

Reputation: 303

How to turn an array of an array of doubles into string, Swift

I have an array of array of doubles. How do you convert that to a single string printed on the console?

I need to create a function that takes in an array of arrays of doubles as a parameter and returns a string object. Then loop through each of the inner arrays and concatenate each element of the inner arrays into a String object. But I'm not sure how to go about that.

var array1 = [2.6, 6.7, 7.2, 4.1, 3.1]
var array2 = [1.2, 3.5, 2.8, 4.5, 6.4]
var array3 = [1.2, 1.3, 1.4, 1.5, 1.6]

var nestedArray = [array1, array2, array3]

This is the code I have... but i don't know how to do a for loop that would give me my answer...

func nestedFunction(nestedArray: [[Double]]) -> String {
    var stringVar: String = ""

    ( for loop here... )

    return stringVar
}

print(nestedArrayFunction(nestedArray))

The expected output should be a string object, with no brackets

Upvotes: 3

Views: 2568

Answers (4)

dfrib
dfrib

Reputation: 73176

From the description in your question it seems as if the argument of nestedFunction(...) should be an array of arrays with double valued elements ([[Double]]) rather than an array with double valued elements ([Double]).

You can make use of .flatten() to your simply nested array nestedArray in nestedFunction(...), and thereafter e.g. reduce to transform the Double valued elements of the flattened array to one concenated String.

var array1 = [2.6, 6.7, 7.2, 4.1, 3.1]
var array2 = [1.2, 3.5, 2.8, 4.5, 6.4]
var array3 = [1.2, 1.3, 1.4, 1.5, 1.6]

var nestedArray = [array1, array2, array3]

func nestedFunction (nestedArray: [[Double]])-> String {
    return String(nestedArray
        .flatten()
        .reduce("") { $0 + ", " + String($1) }
        .characters.dropFirst(2))
}

let asString = nestedFunction(nestedArray)
    // asString = "2.6, 6.7, 7.2, 4.1, 3.1, 1.2, 3.5, 2.8, 4.5, 6.4, 1.2, 1.3, 1.4, 1.5, 1.6"

As an alternative, if you're set on using a for loop, you can use for ... in on the flattened array, e.g.:

var array1 = [2.6, 6.7, 7.2, 4.1, 3.1]
var array2 = [1.2, 3.5, 2.8, 4.5, 6.4]
var array3 = [1.2, 1.3, 1.4, 1.5, 1.6]

var nestedArray = [array1, array2, array3]

func nestedFunction (nestedArray: [[Double]])-> String {
    var stringVar: String = ""
    var isFirstElement = true
    for elem in nestedArray.flatten() {
        stringVar += (isFirstElement ? "" : ", ") + String(elem)
        isFirstElement = false
    }
    return stringVar
}

let asString = nestedFunction(nestedArray)
    // asString = "2.6, 6.7, 7.2, 4.1, 3.1, 1.2, 3.5, 2.8, 4.5, 6.4, 1.2, 1.3, 1.4, 1.5, 1.6"

Note that due to limited floating point precision, some double values might end up with a "messy" String representation (e.g. 2.6 might end up as 2.6000000000000001) when using the direct String initializer (String($1) and String(elem) above, in the first and second method, respectively). To redeem this you could set a fixed number of fraction digits for the String representation of your Double values, using the following String initializer:

String(format: "%.3f", myDoubleValue)
    /*             \
           number of fraction digits (3 here) */

E.g., replace String($1) and String(elem) in the methods above by String(format: "%.3f", $1) and String(format: "%.3f", elem), respectively, for some number of fraction digits of your choice. The Double values will be rounded to the number of supplied fraction digits.

Upvotes: 0

Claus Jørgensen
Claus Jørgensen

Reputation: 26347

If you want it without the brackets:

let string = nestedArray.flatMap { (array) in
    array.flatMap { String($0) }.joinWithSeparator(",")
}.joinWithSeparator(",")

Output:

"2.6,6.7,7.2,4.1,3.1,1.2,3.5,2.8,4.5,6.4,1.2,1.3,1.4,1.5,1.6"

Mind that using , as separator isn't localisation proof, and in French locale would result in a string like "2,6,6,7,7,2" and so on.

Upvotes: 1

Toluwa Ajibola
Toluwa Ajibola

Reputation: 1

First, you will need to concatenate, or combine, the arrays you have. You can do that simply by creating another array, like this;

let fullArray = array1 + array2 + array3

Next, you can print the full array,

print(fullArray)

Which would give you something like

[2.6000000000000001, 6.7000000000000002, 7.2000000000000002, 4.0999999999999996, 3.1000000000000001, 1.2, 3.5, 2.7999999999999998, 4.5, 6.4000000000000004]

or you can create a string representation of this array using the .joinWithSeparator(String: String) method, like so

let stringRepresentation = fullArray.joinWithSeparator(",")
print(stringRepresentation)

Hope this helps!


Upvotes: 0

Alexander
Alexander

Reputation: 63167

Array conforms to CustomStringConvertible, so you can just use its description property:

nestedArray.description

output:

[[2.6000000000000001, 6.7000000000000002, 7.2000000000000002, 4.0999999999999996, 3.1000000000000001], [1.2, 3.5, 2.7999999999999998, 4.5, 6.4000000000000004], [1.2, 1.3, 1.3999999999999999, 1.5, 1.6000000000000001]]

This is what the regular print function uses to print such arrays.

Upvotes: 0

Related Questions