View previous topic :: View next topic |
Author |
Message |
dmcmillan
Joined: 04 Jul 2006 Posts: 22
|
Posted: Mon Jun 15, 2009 11:22 am Post subject: a seemingly trivial read statement question |
|
|
Hello,
is there a way of executing a read statement so that you stay on the same line of the input file as opposed to moving to the next line down?
This seems trivial but I can't seem to find a solution that doesnt involve messing about with strings.
Thanks
DM |
|
Back to top |
|
 |
JohnHorspool
Joined: 26 Sep 2005 Posts: 270 Location: Gloucestershire UK
|
Posted: Mon Jun 15, 2009 11:58 am Post subject: |
|
|
DM,
As you have alluded to, messing with strings is the way to go. But is that such a problem? Once you have the string, you can interrogate it for number of items, their positions in the string, and do selective internal reads as required. |
|
Back to top |
|
 |
dmcmillan
Joined: 04 Jul 2006 Posts: 22
|
Posted: Mon Jun 15, 2009 12:04 pm Post subject: |
|
|
Hello John,
Thanks so much for the prompt reply.
OK so it looks like i need to read up on my string operations! I was just wondering if it was possible to avoid this by using a read that would stay on the same line. But if not i should bite the bullet and understand string operations!
Thanks again,
David McMillan |
|
Back to top |
|
 |
dmcmillan
Joined: 04 Jul 2006 Posts: 22
|
Posted: Mon Jun 15, 2009 1:44 pm Post subject: |
|
|
hi could i ask, are the (char) functions you are referring to intrinsic?
i cant seem to find an intrinsic funtion for what i want to do (i.e. read whole line as a string and count the number of seperate entries, assigning each to an array element). |
|
Back to top |
|
 |
JohnHorspool
Joined: 26 Sep 2005 Posts: 270 Location: Gloucestershire UK
|
Posted: Mon Jun 15, 2009 2:47 pm Post subject: |
|
|
Unfortunately not (at least I think so).
I wrote my own code to extract items (or sub-strings) from a long string more than 25 years ago, and have used it ever since. The routine places start and end positions for each sub-string into an integer array, considering spaces to be de-limiters.
Code: |
SUBROUTINE GETTERM(STRING,LENGTH,ITERM,NITEM)
C Input
C STRING = input string
C LENGTH = length of string
C Output
C ITERM = integer array containing sub-string start and endS
C NITEM = number of sub-string items
DIMENSION ITERM(2,65)
CHARACTER*(*) STRING
KOUNT=0
NITEM=0
10 IF (NITEM.EQ.65) RETURN
IF (KOUNT.EQ.LENGTH) RETURN
KOUNT=KOUNT+1
IF (STRING(KOUNT:KOUNT).EQ.' ') GO TO 10
NITEM=NITEM+1
ITERM(1,NITEM)=KOUNT
20 IF (KOUNT.EQ.LENGTH) THEN
ITERM(2,NITEM)=KOUNT
RETURN
END IF
KOUNT=KOUNT+1
IF (STRING(KOUNT:KOUNT).EQ.' ') THEN
ITERM(2,NITEM)=KOUNT-1
GO TO 10
END IF
GO TO 20
END |
|
|
Back to top |
|
 |
dmcmillan
Joined: 04 Jul 2006 Posts: 22
|
Posted: Mon Jun 15, 2009 3:19 pm Post subject: |
|
|
Thank you again John. Will be sure to give you a mention if any of the outputs reach the public domain.
David |
|
Back to top |
|
 |
IanLambley
Joined: 17 Dec 2006 Posts: 506 Location: Sunderland
|
Posted: Mon Jun 15, 2009 6:43 pm Post subject: |
|
|
If you just want to stay on the same line, try using the backspace command after the read which will position the input pointer at the start of the line just read.
If you want to extract things from a line a bit at a time, then have a look at the post:
"Trouble Reading A .CSV file"
I posted several routines for this. Read the last part as it includes a couple of bug fixes.
Regards
Ian |
|
Back to top |
|
 |
LitusSaxonicum
Joined: 23 Aug 2005 Posts: 2402 Location: Yateley, Hants, UK
|
Posted: Mon Jun 15, 2009 11:16 pm Post subject: |
|
|
David,
If you are thinking of parsing variable length lines with variable amounts of data, give some thought as to how they are created. For example, if they are created by Excel or a Fortran program, then the "separator text" between the individual numeric data items is likely to be simple (e.g. one or more spaces, a comma, or a comma followed by spaces, are obvious examples).
If you create the files yourself then you are similarly likely to conform to some easily-defined set of rules.
The real problems arise if the data files are generated by someone else! Getting people to conform to what to you may seem to be simple and straightforward rules, and which may seem to them to be arbitrary and unnecessarily complex, is difficult. The amount of error handling again depends on who you expect to fix it if it doesn't work (e.g. an aide memoir to self is not any use to a third party user).
It isn't just the character handling routines you will have to get used to (afresh?), but also internal reads and writes.
Eddie |
|
Back to top |
|
 |
|