reading data from a table in swift


(Paul) #1

Hi

I am almost a complete noob to Swift and I am writing my first app.

I need to read a value from a table and return that to the app to do a few calculations but I am so new I have no idea what is the best method to do this.

Specifically there are two tables (currently in microsoft excel) that i need to “read” and get a value from . The values in these tables never change so I can convert them into an array or other data structure that would make sense in Swift.

The first table is about 25 x 25 (rows and columns).

I need to read down the first column (A) to find the value that is equal to or the next larger value from what the user had input, and then I read across the first row (1) to find the value that is equal to or the next larger value. The cell where the row and column intersect contains a value and I want to extract that value and use it in a few calculations.

Can anyone help point me in a direction to get started with that?

The second table is 20 X 13 (rows v columns).

Similar to the first table, I need to read down the first column (A) to find the value that is equal to or the next larger value from what the user had input…unlike the first table, now I have to read across that specified row to find a cell value that is equal to or the next larger value to what the user input, and then read up to the column to the top row and read the value from the cell in row (0)

Again, I would appreciate any help about how to accomplish this.

Thanks in advance

Paul


(Saagar Jha) #2

Here’s a solution I came up with, which should generalize for any condition and for arrays with dimensions >= 1 by 1. I’m not sure if there’s a Swiftier™ way to iterate over 2D arrays; if there is I’d like to hear it!

// Find the index of the first row in table in the column specified where the condition is true
func firstRow(table: [[Int]], forColumn column: Int, where predicate: (Int) -> Bool) -> Int {
  for row in 0..<table.count {
    if predicate(table[row][column]) {
      return row
    }
  }
  return -1
}

// Find the index of the first column in table in the row specified where the condition is true
func firstColumn(table: [[Int]], forRow row: Int, where predicate: (Int) -> Bool) -> Int {
  for column in 0..<table[row].count {
    if predicate(table[row][column]) {
      return column
    }
  }
  return -1
}

let input1 = 0 // Your user input here
let input2 = 0 // Your user input here
let table1 = [[1, 2], [3, 4]] // Your Table 1 here
let table2 = [[5, 6], [7, 8]] // Your Table 2 here

let row1 = firstRow(table: table1, forColumn: 0, where: { $0 >= input1 })
let column1 = firstColumn(table: table1, forRow: 0, where: { $0 >= input1 })
let value1 = table1[row1][column1]

let row2 = firstRow(table: table2, forColumn: 0, where: { $0 >= input2 })
let column2 = firstColumn(table: table2, forRow: row2, where: { $0 > input2 })
let value2 = table2[row2][column2]

Hope this helps,
Saagar Jha

···

On Nov 26, 2016, at 12:33, Paul via swift-users <swift-users@swift.org> wrote:

Hi

I am almost a complete noob to Swift and I am writing my first app.

I need to read a value from a table and return that to the app to do a few calculations but I am so new I have no idea what is the best method to do this.

Specifically there are two tables (currently in microsoft excel) that i need to “read” and get a value from . The values in these tables never change so I can convert them into an array or other data structure that would make sense in Swift.

The first table is about 25 x 25 (rows and columns).

I need to read down the first column (A) to find the value that is equal to or the next larger value from what the user had input, and then I read across the first row (1) to find the value that is equal to or the next larger value. The cell where the row and column intersect contains a value and I want to extract that value and use it in a few calculations.

Can anyone help point me in a direction to get started with that?

The second table is 20 X 13 (rows v columns).

Similar to the first table, I need to read down the first column (A) to find the value that is equal to or the next larger value from what the user had input…unlike the first table, now I have to read across that specified row to find a cell value that is equal to or the next larger value to what the user input, and then read up to the column to the top row and read the value from the cell in row (0)

Again, I would appreciate any help about how to accomplish this.

Thanks in advance

Paul
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users