Thanks a lot, it works. The second loop gave nothing new:

julia> o=open("output_4b.txt")
IOStream(<file output_4b.txt>)

julia> cnt=0
0

julia> for line=eachline(o)
         cnt=max(cnt, length(split(line, ",")))
       end

julia> cnt
24

julia> for line=eachline(o)
         cnt=max(cnt, length(split(line, ",")))
       end

julia> cnt
24
Paul
W dniu 2014-11-30 o 19:28, Andrew Dolgert pisze:
Hi Paul,

If memory usage is a challenge, then the only way to know the number of columns is to read the whole thing twice. Loop through it once something like:

cnt=0
for line=eachline(file)
  cnt=max(cnt, length(split(line, “,”)))
end

and then loop again as before.

As to the rotation of the array, I chose the order that was fastest for reading the file, but you might do better to choose the order fastest for working with the data. Julia uses Fortran order. The other order would use “data=zeros(Int, (linecnt,4)” and “data[idx,1:length(m)]=linecnt”. In either case, change “data=Array(...” to “data=zeros(“ so that you don’t get uninitialized entries. Sorry about that.

- Drew

On Nov 30, 2014, at 8:03 AM, Paul Analyst <[email protected] <mailto:[email protected]>> wrote:

Andrew, Big Thx

My number col now is 24, but in future is unknow. How to know colcnt ? (in script is 4)

(Now data after script is rotated , real data is data' ;) )
Paul



W dniu 2014-11-29 o 07:56, Andrew Dolgert pisze:
The readcsv command only pads <https://github.com/JuliaLang/julia/blob/ab1f287906327941f401ed85f0fa6fd549b17bbf/base/datafmt.jl#L173> for the Any type and AbstractString type. The skipblanks option doesn't appear to help. If the file is as regular as you show, then parsing might not be so bad.

function readit(filename)
        # Just one way to get the number of lines. Needs unix.
linecnt=int(match(r"\d+", readall(`wc -l z.txt`)).match)
data=Array(Int, (4,linecnt))
open(filename) do fileio
anint=r"\d+"
idx=1
for line=eachline(fileio)
 m=matchall(anint, line)
   if m!=nothing
 data[1:length(m),idx]=map(int, m)
   idx+=1
end
end
end
data
end



Reply via email to