reading strings with embedded "/" characters 
Author Message
 reading strings with embedded "/" characters

I have text files with lines such as

2/25/2001 12.2 10.4
12/2/2000  2.0 3.0

I would like to read strings from each line and then read the
numerical data from each string. The problem is that

character (len=*), parameter :: input_line="2/25/2001 12.2 10.4"
character (len=100)  
read (input_line,*) s1,s2,s3

will not read "2/25/2001" into s1, since "/" is recognized as a delimiter.
What I do is replace the "/" characters with some other character
such as "?", read the strings, and then replace "?" with "/".
This is ugly. Is there a way to tell the compiler not to treat "/" as a
delimiter?



Thu, 14 Aug 2003 23:39:14 GMT  
 reading strings with embedded "/" characters

Quote:
>I have text files with lines such as

>2/25/2001 12.2 10.4
>12/2/2000  2.0 3.0

>I would like to read strings from each line and then read the
>numerical data from each string. The problem is that

>character (len=*), parameter :: input_line="2/25/2001 12.2 10.4"
>character (len=100)  
>read (input_line,*) s1,s2,s3

>will not read "2/25/2001" into s1, since "/" is recognized as a delimiter.
>What I do is replace the "/" characters with some other character
>such as "?", read the strings, and then replace "?" with "/".
>This is ugly. Is there a way to tell the compiler not to treat "/" as a
>delimiter?

Use a formatted read for the string.  You can still read the numeric
data as list-directed, reading from input_line after the initial
string.

List-directed input of strings very often carries surprises; I've
learned to avoid it.

Once you've read an input line into the string input_line, you've got
the ability to do just about anything you want with it.  You don't
even need to use read to get S1: you can do

      s1 = input_line(1:9)

And if S1 isn't always the same length (maybe way in the future you'll
be working on Y10K issues and will have the date 2/25/10002), you can
use index to find the first blank.

Ken Plotkin



Fri, 15 Aug 2003 00:57:18 GMT  
 reading strings with embedded "/" characters

Quote:

> Use a formatted read for the string.

Just a terminology correction here.  List-directed reads *ARE*
formatted reads.  They use list-directed formatting.  What is
wanted here is to use an explicit format instead of a list-directed
one.

Other than that terminology nit, I'll just second what Ken said.

--
Richard Maine
email: my last name at domain
domain: qnet dot com



Fri, 15 Aug 2003 03:12:10 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Checking strings for "bad" characters

2. binary scan: "character string"?

3. string.join(["Tk 4.2p2", "Python 1.4", "Win32", "free"], "for")

4. '"""' and linefeed characters

5. string match fails for strings containing "["

6. Characters "?", "?", "ΓΌ" shown in wrong font

7. replace string AFTER "size","initial", "next"

8. String with " character

9. newbie question: the " character inside a string

10. The "LAMP" character

11. VW and "foreign" characters

12. "hexadecimal characters" in ST.

 

 
Powered by phpBB® Forum Software