[FORTRAN] Character Substring Extracting

  • Context: Fortran 
  • Thread starter Thread starter Nicolaus
  • Start date Start date
  • Tags Tags
    Fortran
Click For Summary

Discussion Overview

The discussion revolves around extracting substrings from a date formatted as "d/m/y" in Fortran. Participants explore methods for validating the format and extracting the day, month, and year components from the string. The conversation includes both coding issues and broader data handling techniques.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant requests assistance with extracting substrings from a date string and describes the intended functionality of their subprogram.
  • Another participant points out that the variables "count" and "yes" need to be initialized, suggesting that the position of the first slash can help identify the day substring.
  • A third participant critiques the naming of the logical variable "yes," proposing "isValid" as a more sensible alternative and emphasizes tracking the positions of the slashes for substring extraction.
  • A different participant introduces the Fortran intrinsic function INDEX as a method to find the positions of the slashes, explaining how to extract the day, month, and year based on those indices.
  • One participant expresses satisfaction by stating they have solved their issue after receiving input.
  • A new topic is introduced regarding converting a text file with date data into CSV format, highlighting the challenge posed by backslashes as delimiters.
  • Another participant suggests using a specific FORMAT statement for reading the structured data, indicating that consistent column placement of slashes allows for easier data handling without parsing.

Areas of Agreement / Disagreement

Participants generally agree on the need for proper initialization of variables and the importance of tracking slash positions for substring extraction. However, there is no consensus on the best approach to handle the conversion of text file data into CSV format, as this introduces a new topic with varying suggestions.

Contextual Notes

The discussion includes various assumptions about the structure of the input data and the handling of string operations in Fortran. Some participants' suggestions depend on specific formatting of the input data, which may not be universally applicable.

Who May Find This Useful

Readers interested in Fortran programming, string manipulation, and data conversion techniques may find the insights shared in this discussion beneficial.

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 ·
Replies
12
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
4
Views
3K
  • · Replies 13 ·
Replies
13
Views
4K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 12 ·
Replies
12
Views
16K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
9K