6

I've a txt including some data in the following format.

AYGA:GKA:GOROKA:GOROKA:PAPUA NEW GUINEA:06:04:54:S:145:23:30:E:5282
AYLA:LAE::LAE:PAPUA NEW GUINEA:00:00:00:U:00:00:00:U:0000
AYMD:MAG:MADANG:MADANG:PAPUA NEW GUINEA:05:12:25:S:145:47:19:E:0020

How to separate each item distinguished with colons(":") and how to load each section to an array like in the example below?

var array1 = ["AYGA", "AYLA", "AYMD"]
var array2 = ["GKA", "LAE", "MAG"]
var array3 = ["GOROKA", "", "MADANG"]
var array4 = ["GOROKA", "LAE", "MADANG"]
var array5 = ["PAPUA NEW GUINEA", "PAPUA NEW GUINEA", "PAPUA NEW GUINEA"]
var array6 = ["06", "00", "05"]
var array7 = ["04", "00", "12"]
var array8 = ["54", "00", "25"]
var array9 = ["S", "U", "S"]
var array10 = ["145", "00", "145"]
var array11 = ["23", "00", "47"]
var array12 = ["30", "00", "19"]
var array13 = ["E", "U", "E"]
var array14 = ["5282", "0000", "0020"]
do it better
  • 4,627
  • 6
  • 25
  • 41
  • I assume you don't want the numbers included ? Or the E / S / U ? Have you tried regular expression ? – DogCoffee Oct 03 '15 at 07:26
  • I want the numbers and lettters. These numbers are the coordinates. I want to segment only with colons. I've updated my post. – do it better Oct 03 '15 at 07:39

3 Answers3

8

What you are trying to do is called a transposition. Turning an array that looks like:

[[1, 2, 3], [4, 5, 6]]

into an array that looks like:

[[1, 4], [2, 5], [3, 6]]

To do this, let's define a generic function for transposition and apply it to your problem

// Import the text file from the bundle

guard
    let inputURL = NSBundle.mainBundle().URLForResource("input", withExtension: "txt"),
    let input = try? String(contentsOfURL: inputURL)
    else { fatalError("Unable to get data") }

// Convert the input string into [[String]]
let strings = input.componentsSeparatedByString("\n").map { (string) -> [String] in
    string.componentsSeparatedByString(":")
}

// Define a generic transpose function.
// This is the key to the solution.

public func transpose<T>(input: [[T]]) -> [[T]] {
    if input.isEmpty { return [[T]]() }
    let count = input[0].count
    var out = [[T]](count: count, repeatedValue: [T]())
    for outer in input {
        for (index, inner) in outer.enumerate() {
            out[index].append(inner)
        }
    }

    return out
}

// Transpose the strings
let results = transpose(strings)

You can see the results of the transposition with

for result in results {
    print("\(result)")
}

Which generates (for your example)

["AYGA", "AYLA", "AYMD"]
["GKA", "LAE", "MAG"]
["GOROKA", "", "MADANG"]
["GOROKA", "LAE", "MADANG"]
["PAPUA NEW GUINEA", "PAPUA NEW GUINEA", "PAPUA NEW GUINEA"]
["06", "00", "05"]
["04", "00", "12"]
["54", "00", "25"]
["S", "U", "S"]
["145", "00", "145"]
["23", "00", "47"]
["30", "00", "19"]
["E", "U", "E"]
["5282", "0000", "0020"]

This has the advantage of not depending on the number of arrays that you have, and the number of subarrays is taken from the count of the first array.

You can download an example playground for this, which has the input as a file in the playground's resources.

Abizern
  • 146,289
  • 39
  • 203
  • 257
  • 1
    Thanks so much for this, but syntax for Swift 4 for `transpose` has changed: `fileprivate func transpose(input: [[T]]) -> [[T]] { if input.isEmpty { return [[T]]() } let count = input[0].count var out = [[T]](repeating: [T](), count: count) for outer in input { for (index, inner) in outer.enumerated() { out[index].append(inner) } } return out }` – Crashalot May 26 '18 at 07:17
1

Here is another alternative that handles different newline characters well and doesn't require any hard coding to get the correct number of arrays. The number of colon-separated components is read from the first line.

let input = "AYGA:GKA:GOROKA:GOROKA:PAPUA NEW GUINEA:06:04:54:S:145:23:30:E:5282\nAYLA:LAE::LAE:PAPUA NEW GUINEA:00:00:00:U:00:00:00:U:0000\nAYMD:MAG:MADANG:MADANG:PAPUA NEW GUINEA:05:12:25:S:145:47:19:E:0020"
var arrays: [[String]]?
input.enumerateLines { (line, _) in
    let chunks = line.componentsSeparatedByString(":")

    if arrays == nil {
        arrays = [[String]](count: chunks.count, repeatedValue: [String]())
    }

    chunks.enumerate().forEach { item in
        arrays?[item.index].append(item.element)
    }
}
hennes
  • 9,147
  • 4
  • 43
  • 63
0

Perhaps more generically (and with a zip like behaviour):

extension Sequence where
    Element: Collection,
    Element.Index == Int,
    Element.IndexDistance == Int
{
    public func transposed(prefixWithMaxLength max: Int = .max) -> [[Element.Element]] {
        var o: [[Element.Element]] = []
        let n = Swift.min(max, self.min{ $0.count < $1.count }?.count ?? 0)
        o.reserveCapacity(n)
        for i in 0 ..< n {
            o.append(map{ $0[i] })
        }
        return o
    }
}

Now we can use it like so:

[0..<5, 10..<20, 100..<200]
    .map(Array.init)
    .transposed()

... which returns:

[[0, 10, 100], [1, 11, 101], [2, 12, 102], [3, 13, 103], [4, 14, 104]]
Milos
  • 2,728
  • 22
  • 24