Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[FORTRAN] Character Substring Extracting

  1. Mar 26, 2013 #1
    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.
    end if
    end do

  2. jcsd
  3. Mar 26, 2013 #2


    User Avatar
    Science Advisor
    Homework Helper

    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.
  4. Mar 26, 2013 #3


    Staff: Mentor

    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.
  5. Mar 27, 2013 #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:)
  6. Mar 28, 2013 #5
    Thanks; solved it!
  7. May 27, 2013 #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...

    yy mm dd hh rain temperature wind speed
  8. May 28, 2013 #7


    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    As long as the structure of the data is the same throughout the text file, then read the data with the proper FORMAT statement:

    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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook