vLookup In App?

Hey guys, very new here. Never coded Swift, and just beginning with C++, but wanting to make a specific app I've had in mind for a long time. It involves a lot of spreadsheet math and equations, and I was simply wondering if there was some way to reference a table/chart like you can do in Excel?


Basically, based upon 3 numbers that a user inputs, I want a chart to be populated in a very specific manner, and then I want equations to call the values in the chart.


As an example:


If a user inputs "10 RPE" and "2 Reps", I want it to take a certain number (Load) and multiply it by 0.96. Now, I understand that I could just use a bunch of switch case or if else statements, but that would be A LOT of clutter, and I don't want my code to be long and confusing. The reason I say this is because there will be dozens of charts among hundreds of programs which customize these charts. I can post the spreadsheet here if you guys would like to see what I'm referring to.

Replies

Anyone?

I suspect that the reason no one has responded is that your question is cast in terms of spreadsheets, which are a bit hard to grok when you’re used to traditional programming languages. So I dusted off my copy of Numbers and looked up the

VLOOKUP
function. It seems to be equivalent to Swift’s array subscripting. For example:
let sheet: [[Int]] = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],   
]
let row = 1
let column = 2
print(sheet[row][column])  // prints “6”

If you read the documentation on arrays and subscripts, that might put you on the right path.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thank you, I'll be sure to check out the documentation. Now, again, I'm new to Swift, so forget the silly questions, but can you generate array numbers based upon other array inputs?


Basically, I want to have people input 3 numbers (which will be a part of the arrays), and have the rest of the array numbers generate in a certain manner from there. For example, if I want the user to input row 1, column 1, and from there, it would make, say, row 2, column 1 two more, and row 2, column 2 one more, could I do something like that?


I assume I can.

Additionally, how does the array formatting go?


Why would row 1, column 2 be 6? Are columns horizontal, and rows vertical here? Also, is there a 3-number limit per row of code? If I add a 4th number to each row, it still returns 6.


SO confused regarding the order. It makes zero sense why the rows/columns aren't logically ordered.

If you look at Quinn's example:


let sheet: [[Int]] = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
]


and compare it to a spreadsheet, it would look like this:

        A       B       C
1       1       2       3
2       4       5       6
3       7       8       9

If we add a 4th number to each row, the spreadsheet would look like this:

        A       B       C       D
1       1       2       3       95
2       4       5       6       96
3       7       8       9       97

which would correspond to:

let sheet: [[Int]] = [
    [1, 2, 3, 95],
    [4, 5, 6, 96],
    [7, 8, 9, 97],
]


Columns are vertical, but there are no "real columns" as in a spreadsheet. Instead, every row "contains" all numbers in the row. It is still easy to get a certain cell (or element, as we call it), since you first get the row you want and then get the column you want from that.


There is no limit per row of code, but the second column of the first row will always be the same, even if you add numbers at the end of the row.


One thing to remember is that in Swift (and most programming languages), rows and columns start on 0, while they start on 1 in most spreadsheets. Is this what confuses you?

No, the one thing I know is it starts at zero. I started the route at zero, I think I forgot to start at zero for the columns, though. I'll have to try when I have a computer near me tonight! I just wanted to make sure there was no limit, as I wanted 11 columns and 9 rows.