0

I want to change subscript coordinates in an Array of Arrays [[Int]] in order to be able to address each value in a way similar to a spreadsheet, so assigning values, formulas across cells, etc.

Currently to address each value I need to subscript like table[2][1] = 12 or similar. I would like to subscript like a spreadsheet table[a][2] = 12, so that I can adapt long and complicated formulas in bigger speadsheets to similar tables using the same system used in spreadsheets.

The question is: how to change the subscript system in an efficient way? I send a simplified example to ilustrate the point

class ViewController: UIViewController {

var table = [[0,  1,  2,  3],
             [1, 32, 44, 25],
             [2, 12, 66, 43],
             [3,  3,  4,  5]]


override func viewDidLoad() {
    super.viewDidLoad()

    print(table[2][1])  // 12
    table[1][1] = 100
    table[3][3] = table[1][1] * table[3][1] * 10
    print(table[3][3])

    printArray(table: table, j: 3)


}

// MARK: - Function

func printArray(table: [[Int]], j:Int) {
    for i in 0...j {
        print(table[i])
    }
}

}

6
  • This is the most efficient way. What you are asking for is how to make it easily understandable for viewing so that you can compare it with a spreadsheet grid. I wouldn't recommend changing your code to suit viewing needs. Also, I'm curious to know where you would be using hardcoded subscripts. Commented Oct 22, 2018 at 9:07
  • I see your point but there are many, long and complicated excel formulas to be translated, with IFs and other formulas concatenated, so I need to adapt them easily and without errors to my IOS, so a tool to change subscript would help a lot Commented Oct 22, 2018 at 9:12
  • For example of formula: =SI(Y($Y$17>1;AA9>0;$H$10<=Tablas!$C$9);"NORMAL";SI(Y($Y$17>1;AA9>0;$H$10>Tablas!$C$9;$H$10<=Tablas!$C$10);"SUPERADA";SI(Y($Y$17>1;AA9>0;$H$10>Tablas!$C$10);"ROTURA";"N/A"))) Commented Oct 22, 2018 at 9:22
  • 1
    You can use dictionary with letter keys and array values instead of array of arrays. Commented Oct 22, 2018 at 9:26
  • Why not create a wrapper class/struct containing your array of arrays as the data source and with different functions that accepts cell coordinates in a format you want like (‘C’, 9) or (‘$C$9’) etc Commented Oct 22, 2018 at 9:47

1 Answer 1

2

The closest solution I found is to use enums, like here:

enum Column:Int {
    case a
    case b
    case c 
    // and so on
}

extension Array {
    subscript(col:Column) -> Element {
        get {
            return self[col.rawValue]
        }
        set(newValue) {
            self[col.rawValue] = newValue
        }
    }
}


var table = [[0,  1,  2,  3],
            [1, 32, 44, 25],
            [2, 12, 66, 43],
            [3,  3,  4,  5]]


table[.a][2] = 12
let val = table[.a][2]
print (val)

Nevertheless, there are two little drawbacks:

  • you first have hard-code all the "column" letters you want to access to the Column enum.
  • then, something like table[5][.c] and table[.a][.b] will also be allowed - so you could access the rows via letter, not only the columns
Sign up to request clarification or add additional context in comments.

4 Comments

Andreas I see a major problem, your solution is table[row] [column], but I need table [column] [row] the same as in Excel
So, with the first subscript you want to access the "inner" array, and with the second the "outer"?
what i mean is: Table > I would paste it from Excel in the matrix var table of IOS, very convenient. So columns and rows as indicated in my example. Formulas > I need Table [column][row] like in excel so to copy the formulas with the same subscripts
=SI(Y($Y$17>1;AA9>0;$H$10<=Tablas!$C$9);"NORMAL";SI(Y($Y$17>1;AA9>0;$H$10>Tablas!$C$9;$H$10<=Tablas!$C$10);"SUPERADA";SI(Y($Y$17>1;AA9>0;$H$10>Tablas!$C$10);"ROTURA";"N/A")))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.