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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

strings - simple right?

Status
Not open for further replies.

wigglytoes

Technical User
Nov 20, 2012
2
CR
Hi:
I actually used to make a living programming in Fortran. 30 years ago.
Therefore I now fall in the category of having forgotten way more than I know.

I hope you can help me out. This should be easy for the pros.

I have one string defined as
CHARACTER*80 LINES(50)
I have another string defined as
CHARACTER*4 FOUR(20)

In the middle of the program that I'm (trying to) write,
I want to stuff the contents of LINES(N) into all of FOUR

I can't seem to get it right.
An implied DO loop?
A WRITE statement?
SOmehow and EQUIVALENCE

I'm really only trying to look at the first four characters of one of the LINES.

thanks

Wiggly
 
Code:
program chararray
  character(len=80), dimension(50) :: lines
  character(len= 4), dimension(20) :: four
  
  lines(1)= 'first line...this is the first line'
  lines(2)= 'second lines...this is the second line'
  
  four(1) = '1111'
  four(2) = '2222'
  four(3) = '3333'
  
  four(2) = lines(2)(1:4)
  
  write(*,*) lines(1)
  write(*,*) lines(2)
  write(*,*) four(2)
  
end program chararray
 
I would suppose:

Code:
character*80 Line (50)
character*4 four(20)

integer jcount
interger jLoop
integer j1, j2

...
...
jcount = 25    ! just to define which line you are working on

do jLoop = 1, 20
    j1 = 4 * (jLoop - 1) + 1
    j2 = 4 * (jLoop - 1) + 4
    four (jLoop) = Line (jCount)(j1 : j2)   ! look up 'substrings' in your documentation
enddo
...

Norbert

The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
You both hit the bull's eye.

The key line for me was the syntax of

four(n) = line(n)(1:4) I kept trying other ways to access that area. Like line(n)[1:4] and near misses like that.
will stuff the 1st four characters of each "line" into the "four" string.
and then I can check it with something simple like
If (four(n) .eq. 'good') then ...

thanks guys
 
What would you do, if 'good' would start at another position than 1, 5, 9, 13, ...? Then you will not have 'good' within one four letter string but spread amongst two. If it is possible that your keyword starts anywhere - and you may have more than one keyword, then I would propose the following code:

Code:
logical function KeyWordTest (cKeyword, cLine)

implicit none
character*4 cKeyword
character*80 cLine
integer iPosition           ! position of first character of cKeyword in cLine

iPosition = 0
iPosition = index (cLine, cKeyword)    ! locate substring cKeyword in cLine
if (iPosition .ne. 0) then             ! if iPosition > 0 then cKeyword is found in cLine
    KeywordTest = .true.
else
    KeyWordTest = .false.
endif

return
end

I did put this in a function so you might easily check for a number of keywords, if you want to hunt for 'good', 'well', 'poor', ... Alternatively you might put this in a loop.

Norbert




The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top