# Importing an array from text file and convert it to truth table

I am new to Mathematica, and would like some help in the following operation. I have a truth table stored in a text file. I want to read this in Mathematica, and find its Boolean Rule. Suppose I have two Boolean variables. I am reading the text file in Mathematica like this:

arr=Import["bool.txt","Table"]


Suppose this array is arr=

0 0 0
0 1 1
1 0 1
1 1 1


The third column represents the output, and the first two columns are the states of the inputs. For finding the Boolean rule, I need to convert this array as an association of

table={{0,0}->0,{0,1}->1,{1,0}->1,{1,1}->1}


So that I can infer the Boolean rule using the following function:

BooleanFunction[table,{x,y}]


My problem is, I don't know how to create the variable table, from the array arr. I looked up a bit and think the Association function would be used, but not sure how. I need to do it for multiple text files, and am looking to develop a short program for this functionality. Any help would be appreciated.

Use Map:

table = (Most[#] -> Last[#])& /@ arr

• Thanks a lot, it is as good as the above method. – prabhat Nov 6 '14 at 0:38

You can simply do:

{#, #2} -> #3 & @@@ {{0, 0, 0}, {0, 1, 1}, {1, 0, 1}, {1, 1, 1}}

{{0, 0} -> 0, {0, 1} -> 1, {1, 0} -> 1, {1, 1} -> 1}

• Thanks a lot! So simply a line of code is sufficient I see {#, #2} -> #3 & @@@ arr. – prabhat Nov 6 '14 at 0:37

You could also use Thread:

table = Thread[arr[[All, ;; 2]] -> arr[[All, -1]]]

{{0, 0} -> 0, {0, 1} -> 1, {1, 0} -> 1, {1, 1} -> 1}

• Very useful to know, thanks :) – prabhat Nov 6 '14 at 0:40