When I was at school I was pretty damn good with fortran but now at work I have come across something that I haven't quite encountered before. It is pissing the hell out of me because it is such a simple thing and I feel like the answer is right in front of me. Say I have a data file with numbers in it like as follows:3 7 0 9 8 3 5 2How can I determine how many numbers are in that row? I need to essentially put these into an array but I can't allocate it without knowing how many numbers there are to begin with. Any ideas? It seems like I've tried everything.I essentially need it to move over a line instead of down a line when reading within a loop.[Edited on August 7, 2007 at 3:00 PM. Reason : ]
8/7/2007 2:57:48 PM
do you know that they're going to be a single digit?
8/7/2007 3:11:30 PM
No they won't be single digit, more than likely they will be at least 6 or 7 digits. I just used single digit in this thread for simplicity.
8/7/2007 3:15:10 PM
if all the numbers are of the same length, then you could do a character read-in and then query the length and do the requisite math. and then you can step back (rewind or some such command) and then read the correct number of reals from the line.
8/7/2007 3:18:13 PM
now that i think about it, they wouldn't even need to be the same length. you just have a character variable whose length is definitely going to be longer than the length. you then read in the line. trim the end (i think trim is the right name for that). then get the length. then you can eliminate the blank spaces between the numbers (again i don't remember the exact names for these functions). you can take the length again and the difference would give you the (number of elements - 1).[Edited on August 7, 2007 at 3:35 PM. Reason : so i might be wrong about the second step. looking farther into it]
8/7/2007 3:22:31 PM
I've never used Fortran myself, but I did a quick search for string tokenizer and came across this program that might help: ftp://ftp.simtel.net/pub/simtelnet/msdos/fortran/token.for
8/7/2007 3:39:29 PM
why fortran..... ?you can do this in 1 line in pretty much any modern scripting language Ruby:irb(main):001:0> line = "3 7 0 9 3 5 2"=> "3 7 0 9 3 5 2"irb(main):002:0> line.split(" ").size=> 7
8/7/2007 7:26:42 PM
count the number spaces?if (char = " ") thenx=x+1else...^or that.[Edited on August 7, 2007 at 7:43 PM. Reason : didn't really read that.]
8/7/2007 7:42:35 PM
^yeah, that's what i figured out yesterday after i was off tww, but you'd have to make sure that the next character isn't a space (because in normal formatted output, there are two spaces before positive numbers).
8/8/2007 10:14:56 AM
what dirtymonkey posted is pretty much exactly what he wants.
8/8/2007 10:41:05 AM