# How to define function with a given table of arguments and values?

I have data file with the following structure:

{{100, 201, value1}, {100, 301, value2}, {200, 301, value3}}


How to construct a function f[i_,j_] so that f[100,201]=value1, f[100,301]=value2, f[200,301]=value3. For all other arguments which are not presented in a file one have to obtain zero, i.e. f[100,104]=0?

P.S. This problem arises in connection with atomic physics. It is necessary to construct a function which calls the transitions rates between states i and j tabulated in a file. i=100 means 1S state of hydrogen for example, j=201 = 2p state and etc.

f[_, _]=0;

(f[#, #2] = #3) & @@@ {{100, 201, value1}, {100, 301, value2}, {200, 301, value3}};

f[100,201]
f[100,104]

• Great!!! Thank you! Commented Mar 12, 2018 at 12:49

This is essentially Lookup with a default.

data = {{100, 201, value1}, {100, 301, value2}, {200, 301, value3}}
listOfRules = Most[#] -> Last[#] & /@ data
f = Function[{x, y}, Lookup[listOfRules, Key@{x, y}, 0]]