vb.net - How can I read a real double array from a text file in Visual Basic? -
i think must quite simple, new visual basic. want read set of real numbers text file , create double array them in vb program.
i have found on several places readalltext method, need array double. other codes have found complicated consider simple task. example, in fortran, write subroutine read open statement, cycle, , that's all.
is there simple way doing this?
assuming want end array , start calling file.readalllines
, here's how might if you're sure data valid:
dim lines = file.readalllines(filepath) dim upperbound = lines.getupperbound(0) dim numbers(upperbound) double = 0 upperbound numbers(i) = convert.todouble(lines(i)) next
here's simpler option:
dim lines = file.readalllines(filepath) dim numbers = array.convertall(lines, function(s) convert.todouble(s))
if of data might not valid better option:
dim lines = file.readalllines(filepath) dim numberlist new list(of double) each line in lines dim number double if double.tryparse(line, number) numberlist.add(number) end if next dim numbers = numberlist.toarray()
you can use linq option provided earlier:
dim lines = file.readalllines(filepath) dim numbers = lines.where(function(s) double.tryparse(s, nothing)). select(function(s) double.parse(s)). toarray()
that last option little inefficient because parses each number twice it's succinct , inefficiency not big deal if amount of data small.
Comments
Post a Comment