Silverfrost Forums

Welcome to our forums

a seemingly trivial read statement question

15 Jun 2009 10:22 #4702

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

15 Jun 2009 10:58 #4703

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.

15 Jun 2009 11:04 #4704

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

15 Jun 2009 12:44 #4705

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).

15 Jun 2009 1:47 #4706

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.

      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
15 Jun 2009 2:19 #4707

Thank you again John. Will be sure to give you a mention if any of the outputs reach the public domain.

David

15 Jun 2009 5:43 #4708

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

15 Jun 2009 10:16 #4709

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

Please login to reply.