[FORTRAN] Character Substring Extracting

  • Fortran
  • Thread starter Nicolaus
  • Start date
  • Tags
    Fortran
In summary, the text file contains station data in the following format: yy/mm/dd/hh pressure rain TEMPERATURE wind speed.The data can be read into a variable using the following FORMAT statement: 1010 FORMAT (I4,1x,I2,1x,I2,1x,I2,1x,I5,F3.1,I4,I3)This will create four variables in the program:
  • #1
Nicolaus
73
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
  • #2
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.
 
  • #3
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.
 
  • #4
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:)
 
  • #5
Thanks; solved it!
 
  • #6
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
 
  • #7
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.
 

Related to [FORTRAN] Character Substring Extracting

What is FORTRAN?

FORTRAN (short for FORmula TRANslator) is a high-level programming language commonly used for scientific and engineering applications. It was first developed in the 1950s and has evolved over the years to become one of the most widely used programming languages in computational science.

What is character substring extracting in FORTRAN?

Character substring extracting is a feature in FORTRAN that allows you to extract a portion of a string or character variable. This is useful when you only need a specific part of a longer string, such as a name or a date, for further manipulation or analysis.

How do you extract a character substring in FORTRAN?

To extract a character substring in FORTRAN, you can use the SUBSTRING function. This function takes in three arguments: the string variable, the starting index, and the length of the desired substring. The function will return the specified portion of the string as a new string variable.

Can you extract multiple substrings from a single string in FORTRAN?

Yes, you can extract multiple substrings from a single string in FORTRAN by using the SUBSTRING function multiple times with different starting indices and lengths. You can also use the INDEX function to find the position of a specific character in the string, which can then be used as the starting index for the SUBSTRING function.

What are some common uses for character substring extracting in FORTRAN?

Character substring extracting is commonly used in FORTRAN for data parsing and manipulation. For example, it can be used to extract specific data from a file or to format data before printing it. It is also useful in text processing and data analysis applications.

Similar threads

  • Programming and Computer Science
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
13
Views
4K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
12
Views
15K
Back
Top