2
$\begingroup$

Let's say I have three files containing:

File 1:
x y z
1 4 6 

File 2:
x y z
2 5 9 

File 3:
x y z
5 4 8

My goal is to import the data from a file only if in this file y == 4 (so here I would want to import files 1 and 3).

Is there a way to do that with functional programming? The only solution I can think of currently is to loop through all the files, check if data[[2, 2]] == 4, and append this data to a list if true. Is there a better way to do that?

And if I have to loop, is it better to import all the files as lists and then discard the lists that do not match y == 4? Or is it better to import only the files matching y == 4 in the first place?

$\endgroup$
4
  • $\begingroup$ How do you check if y==4 without importing the files somehow ? $\endgroup$ – b.gates.you.know.what Jan 8 '13 at 10:48
  • $\begingroup$ ok "importing" maybe was not the right word. Let's say I want to keep those files only, and I'm wondering if there is a function with an option allowing me to do something like Import[file, only if y == 4], and if not, if from a list with the data from all files, I can know without looping what files contain y == 4. $\endgroup$ – Sulli Jan 8 '13 at 10:58
  • $\begingroup$ In Mathematica 9 ImageFileApply and ImageFileScan "can be used to read, process, and write successive blocks of data from a very large image file whose size could exceed available memory", i.e. to process without excessive loading. $\endgroup$ – Ernst Stelzer Jan 8 '13 at 14:46
  • $\begingroup$ If its a large file with tag info at the beginning you might want the do raw open/read operations to get the key you need, then import only as needed. For small files my guess is you are just as well off to import the whole thing and descard what yuo dont need. $\endgroup$ – george2079 Jan 8 '13 at 16:46
5
$\begingroup$

If your data is really that simple, then you can use FileNames and Select, e.g.:

Select[Select[FileNames["file*.txt"],Length[FindList[#,{"y","4"},2]]===2&],
StringTake[FindList[#,{"y","4"},2][[2]],{3}]==="4"&]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.