[✓] Read a text file in the Wolfram language?

GROUPS:
 Hi , I am very new to Mathematica. I need to read inputs from the .text file for below code to perform arithematic operation i.e pfused=ad+bcWhen I am trying with Import it is not performing operation, but its reading the text file, please any one help me to resolve the below issue. v = Import["C:\\work\posit.txt", "Text"] For[i = 1, i < Length[v], i = i + 4, a = v[i] ; d = v[i + 3]; b = v[i + 1]; c = v[i + 2]] PArith := Module[{pnuma = a, pnumd=d,pnumb=b,pnumc=c , pfused}, pfused= pnuma*pnumd+pnumb*pnumc ; Print[{pnuma, pnumd, pnumb, pnumc, pfused}]]; Print[N[{pnuma, pnumd, pnumb, pnumc, pfused}, 37]];  Attachments:
3 months ago
6 Replies
 Gustavo Delfino 1 Vote Could you provide a sample "C:\work\posit.txt" ? You probably need to split your text into lines and pay attention to [ ] vs [[ ]]
3 months ago
 Hi Delfino,I have attached my input text file posit.txt below, i have tried with [[ ]], as below For[i = 0, i < Length[v], i = i ++, a = v [[i]]; d = Take v[[i + 3]]; b = Take v[[i + 1]]; c = Take v[[i + 2]]; still I am unable to get the pfused = ad+bcPlease help me to resolve the same. Attachments:
3 months ago
 Grabbar,You have a variety of issues in you question (missing brackets, incorrect calls to Take, referencing indices beyond the last value). Focus first on importing the data: multilineString = Import["posit.txt", "Text"] "64817367.784333 1234.566 2345.567 3456.890 123 234 456 " Then split into lines: listOfStrings = StringSplit[multilineString] {"64817367.784333", "1234.566", "2345.567", "3456.890", "123", "234", "456"} And convert the strings to numbers: listOfNumbers = Map[ToExpression, listOfStrings] {6.48174*10^7, 1234.57, 2345.57, 3456.89, 123, 234, 456} After that you can use the Partition command to split the data in sets of 4 and then apply your formula.
 I added some numbers to your file. You presume they come in blocks of 4 so I created the file with 12 numbers. v = Partition[ReadList["posit_1.txt"], 4] (#[[1]] #[[4]] + #[[2]] #[[3]]) & /@ v I Map a function where I switch the elements of each row I Map on to. The #[[1]] means the first item of the list so its the first * fourth + second * third. Attachments: