Fortran [FORTRAN] Character Substring Extracting

  • Thread starter Thread starter Nicolaus
  • Start date Start date
  • Tags Tags
    Fortran
AI Thread Summary
The discussion focuses on extracting substrings from a date formatted as "d/m/y" in Fortran. Participants highlight the need to check for at least two slashes to ensure the correct format before extracting the day, month, and year. They suggest initializing variables and tracking the positions of the slashes to correctly slice the string. Additionally, they recommend using Fortran's intrinsic string function INDEX to find the slashes' indices efficiently. The conversation also touches on converting text file data into CSV format, emphasizing the importance of consistent data structure for successful parsing.
Nicolaus
Messages
73
Reaction score
0
How would I go about extracting the 3 substrings delimited by the slashes in the date?
The date is a string in the format "d/m/y"
This subprogram checks if there are at least 2 slashes for correct format, and then, if the format is correct, the program would then extract the day, month, and year substrings delimited by the slashes, which is where I'm stuck.

subroutine checkFormat (date, day, month, year, yes)
character*(*) date, day, month, year
logical yes
integer i, count

do while (count .ge. 0 .and. count .lt. 2)
do i = 1, len(date)
if (date(i:i) .eq. "/") then
count = count + 1
end if
end do
if (count .lt. 2) then
yes = .false.
return
end if
end do

return
end
 
Technology news on Phys.org
There are two bugs in your code: you forgot to initialize the variables "count" and "yes" to something.

When you have found the first "/", variable "i" is the position of the "/", so you know the "day" part of the string is date{1:i-1).

You can get the "month" and "year" parts in a similar way. You probably want to remember where the first "/" was, while you are looking for the second one.
 
Nicolaus said:
How would I go about extracting the 3 substrings delimited by the slashes in the date?
The date is a string in the format "d/m/y"
This subprogram checks if there are at least 2 slashes for correct format, and then, if the format is correct, the program would then extract the day, month, and year substrings delimited by the slashes, which is where I'm stuck.

subroutine checkFormat (date, day, month, year, yes)
character*(*) date, day, month, year
logical yes
integer i, count

do while (count .ge. 0 .and. count .lt. 2)
do i = 1, len(date)
if (date(i:i) .eq. "/") then
count = count + 1
end if
end do
if (count .lt. 2) then
yes = .false.
return
end if
end do

return
end

First off, having a logical variable named yes is nonsensical. A better name might be isValid.

When you determine that date has two slashes in it, keep track of their positions. For example, if date is "21/3/13", the first slash is at index 3 and the second slash is at index 4. date(1:2) would hold the day, date(4:4) would hold the month, and date(6:7) would hold the year.
 
Fortran has an intrinsic string function INDEX that returns the index of a character in a string, it can work forwards (from left to right) and backwards (from right to left)

You can find the two indices for the '/ ' character, if they are equal, you do not have two slashes
if they are different, say, k and m, then,

day is in date(:k-1)
month is in date(k+1:m-1)
year is in date(m+1:)
 
Thanks; solved it!
 
Convert text file to csv formatted file

Hi all,
I have station data in a text file and I want to read it using grads and output as a csv file.
The problem is the dates are separated by a backslash...
here's a sample format:

The text file contains the following data.

yy/mm/dd/hh pressure rain TEMPERATURE wind speed
2012/08/06/00 10000 2.5 300 45
...
...
...
...


I want to oputput this in a csv format such that the the years, months, days, and hours are in separated in columns...
like:

yy mm dd hh rain temperature wind speed
 
As long as the structure of the data is the same throughout the text file, then read the data with the proper FORMAT statement:

READ (IUNIT,1010) IYEAR,MONTH,IDAY,IHOUR,PRESS,RAIN,TEMP,WSPEED
1010 FORMAT (I4,1x,I2,1x,I2,1x,I2,1x,I5,F3.1,I4,I3)

This is the basic idea. It will need modification to fit into your program. The idea here is, if the '/' are always in the same column, you can skip reading them and you don't have to make your program parse the data file to determine what is data and what is a separator.
 

Similar threads

Replies
12
Views
3K
Replies
4
Views
2K
Replies
13
Views
4K
Replies
6
Views
2K
Replies
6
Views
2K
Replies
6
Views
2K
Replies
12
Views
15K
Replies
2
Views
2K
Replies
2
Views
9K
Back
Top