begin ;---Read in ASCII file as one-dimensional array of strings filename = "large.txt" lines = asciiread(filename,-1,"string") nlines = dimsizes(lines) print("==================================================") print(filename + " has " + nlines + " lines.") ; ; Assumptions about this file: ; - The very first line starts with a % in the first column. ; - There will be a series of these "%" lines immediately ; followed by a single number with a row count ; - This will immediately be followed by this many rows, with ; 9 columns of data to read. ; - There are no blank lines ; - There are no other stray lines ; ncols = 9 nl = 0 ; line counter do while(nl.lt.nlines) ;---Read the first character of this line first = str_get_cols(lines(nl),0,0) ;---If it's a "%", then increment to next line. if(first.eq."%") then nl = nl + 1 ; increment line counter continue else ;---Otherwise, get the number of rows and read the data. nrows = toint(lines(nl)) nl = nl + 1 ; increment line counter print("==================================================") print("Reading " + nrows + " rows of data.") ; ; Clean up the strings so there's only one space between ; each string, and no extra space at beginning or end. ; This allows us to use str_split_csv to parse this ; chunk of data. str_split_csv expects a single character ; delimiter (a space in this case). ; ; When you do this on a really large file, it might be faster ; to read lines(nl:nl+nrows-1) into a temporary array, rather ; than continually operating on lines itself. ; ; tmp_lines = lines(nl:nl+nrows-1) ; tmp_lines = str_sub_str(tmp_lines," "," ") ; tmp_lines = str_strip(tmp_lines) lines(nl:nl+nrows-1) = str_sub_str(lines(nl:nl+nrows-1)," "," ") lines(nl:nl+nrows-1) = str_strip(lines(nl:nl+nrows-1)) ;---Parse the data into a 2D integer array ; x := tointeger(str_split_csv(tmp_lines," ",0)) x := tointeger(str_split_csv(lines(nl:nl+nrows-1)," ",0)) nl = nl + nrows ;---Print min/max of each column of data. do i=0,ncols-1 print("Column " + (i+1) + " has min/max = " + min(x(:,i)) + \ "/" + max(x(:,i))) end do end if end do end