I’m writing a Julia parser to read in a large output from a Fortran program which is essentially a load of concatenated matrices of differing dimensions. It would be really useful to be able to do something along the lines of readdlm(file,nlines=3) to pull in i.e. the 3x3 matrix you know that follows.
Currently I’m resorting to things like:
celltext=string(readline(f),readline(f),readline(f))
cell=readdlm(IOBuffer(celltext))
And this really doesn’t feel like a very elegant method (not helped as neither readline nor readlines appear to accept ‘number of lines’ as an argument).
Am I missing the Julia way to do things here? Or should I start writing @macros to expand to this level of nitty gritty?
Thank you both!
However, the forming a string with string([readline(STDIN) for i in 1:2]) leads to a type of "Union(ASCIIString,Array{Char,1},UTF8String)[\"1\\n\",\"2\\n\"]" the escaped white space formatting then follows through into the eventual readdlm object (i.e. fields aren’t properly interpreted).
So the working code I have is much more nasty, temporary objects and all kinds of cludge:
function readnlines(f,n)
local lines=""
local i=1
for i=1:n
lines=lines*readline(f)
end
return (lines)
end
readmatrix(f, nlines) = readdlm(IOBuffer(readnlines(f,nlines)))
I think expanding a macro @readnlines(f,nlines) → (readline(f))^nlines) might be more elegant, but I don’t know whether a massive string*string*...string object is efficient to evaluate.
Certainly in general I think a readnlines function is useful.
So would having line ranges in readdlm - currently it supports a ‘skipstart’ option (not documented?) - making this a full line-range object would be nice.
- readdlm in the Julia source
I also found this discussion from last-year on julia-dev, after trying for a while to use the multidimensional / tuple format form of readdlm, I read the source & decided that they were probably talking about prospective changes, not realised ones! (-:
https://groups.google.com/d/msg/julia-dev/PpSy2NQmkG0/cl67UWJec4QJ