Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

count commas in ascii file

Status
Not open for further replies.

hgb1

Programmer
Feb 27, 2007
2
NO
Hello Forum,

I have the following problem and I would be very glad if someone has an idea.

I want to read an ASCII file. Besides many other information the ASCII file contains the following lines at different positions within the file

Length L R = 1 ,3 ,14.48,19.555,24.63
Length L R = 1 ,2 ,100.57,78.965,57.36
Length L R = 1 ,1 ,110,80,50

In this three example lines I only want to read the numbers after the third komma.
So in this case it would be 19.555, 78.965 and 80. The problem is that they are located at different position, depending on the decimals of the prevous numbers.

This means that this idea does not work
IF (line(1:6) .EQ. 'Lenght' ) THEN
READ (line(33:38),'(F8.2)') len

I function would be nice, where the comma is counted and after the third comma the respective number should be used.

Does somebody has an idea how to do this? Another way is also greatly appreciated
 
Hallo hgb1

Try the following. ANUMBER is the figure that you are looking for. The variable aaa reads the first 12 characters 'Length L R ='. After that there are only real numbers, read into r1, r2, r3 and anumber.

program xxx
character*12 aaa
real*8, r1,r2,r3,anumber
open(1,file='filename.txt',status='unknown')
do while(.true.)
read (1,'(a,4f10.0)',end=99) aaa,r1,r2,r3,anumber
enddo
99 close(unit=1)
continue
end
 
I am not sure if this is what you want but the following function should work. Input variables are the string to be sorted through, the character used as the delimiter...
N is the number of CH to go through...
so for the follwoing line
Length L R = 1 ,3 ,14.48,19.555,24.63

a CH = ',' and N = 3 , 19.555 would be returned.

character*(*) FUNCTION PICK(STR,CH,N)
implicit none

integer*2 :: N
CHARACTER*1 :: CH
CHARACTER*(*) :: STR

INTEGER*2 :: I, LEN, J

LEN=LEN_trim(STR)
I = 0
J = 1
DO WHILE (I < N)
IF (STR(J:J).EQ. CH) I = I + 1
J = J + 1
IF (J .GE. LEN) EXIT

ENDDO
IF (J .GE. LEN) THEN
WRITE(*,*) 'ERROR'
ELSE
I = INDEX(STR(J+1:LEN),CH)-1
IF (I.LE.0) I = LEN
PICK = STR(J:J+I)
ENDIF

return
end
 
Try list directed input, that is, let the computer find its own format directed by the list of input variables. This way, the ',' is assumed delimiter. See the following code:

program test
character*8 text1,text2,text3,text4
integer i1,i2
real a1,a2,a3
open(unit=10,file='abfolge.txt')
rewind 10
read (10,*)text1,text2,text3,text4,i1,i2,a1,a2,a3
print*,i1,i2,a1,a2,a3
close (unit=10)
end


this did read your first example line from a file and printed out the proper values on screen.

List directed is somewhat tricky, but it works:
for your strings the blanks blanks are considered delimiters between data so we need four text-dummies to read in 'Line', 'L', 'R' '=', which are padded to character*8 but only take the characters up to the next blank.

For your numericals the commas are the delimiters.

Norbert
 
Hello,

Thank you very much for your replies. I really appreciate the interesting several solutions. It works now quite well.

It was my first post in this forum and I was surprised to get your replies so fast. Hopefully, I may also contribute in future.

By the way: how can I change my denotation from programmer to technical user? I am not a programmer, but unfortunetly I selcetd it, while I subscribed to the forum.
 
Try "Contact us" in the top right corner of this web page.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top