replica nfl jerseysreplica nfl jerseyssoccer jerseyreplica nfl jerseys forums.silverfrost.com :: View topic - INTRINSIC FUNCTION INDEX not scanning from end of string
forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

INTRINSIC FUNCTION INDEX not scanning from end of string

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support
View previous topic :: View next topic  
Author Message
Kenneth_Smith



Joined: 18 May 2012
Posts: 814
Location: Hamilton, Lanarkshire, Scotland.

PostPosted: Fri Feb 27, 2015 12:29 am    Post subject: INTRINSIC FUNCTION INDEX not scanning from end of string Reply with quote

The intrinsic function INDEX scans a string for the position of the first occurrence of a substring.

Here I am searching for the character '9' in the string '1234567890'

Running the code below shows this works fine when the option back is logical .false. - the return value is 9, if .true. i.e. scanning from the end of the sting, when I run this code the return value is 9, which is wrong - it should be 2. Or am I doing something silly here - it's very late here in the UK - well past my bedtime!

Thanks

Ken


Code:
  program test
  implicit none
  character (len=10) w 
  integer i
  logical back
 
  w = '1234567890'
  write(6,'(a)')w
 
  back = .false.
  i = INDEX(w,'9',back )
  write(6,*) i
 
  back = .true.
  i = INDEX(w,'9',back )
  write(6,*) i

  i = INDEX(w,'9')
  write(6,*) i
 
  end program test
Back to top
View user's profile Send private message
mecej4



Joined: 31 Oct 2006
Posts: 1899

PostPosted: Fri Feb 27, 2015 1:00 am    Post subject: Reply with quote

Substring indices are always counted from the left to right, starting with 1. One motivation is that after the index value i has been obtained, one should be able to assume that w(i:i) will equal the character searched for.

I do not know if there are semitic variants of Fortran where things are done otherwise. See the compiler documentation for the intrinsic function INDEX.

To clarify the issue, try running your program with w = '1239567890' instead.
Back to top
View user's profile Send private message
Wilfried Linder



Joined: 14 Nov 2007
Posts: 314
Location: D�sseldorf, Germany

PostPosted: Fri Feb 27, 2015 5:09 am    Post subject: Reply with quote

index(...) finds either the first or the last position of a substring within a string, always counting from left:

Code:
      winapp
      program test
      implicit none
      include <windows.ins>

      integer*4     i,j
      character*20  string

      string = 'abcd47abcd47abcd4711'

      i = index(string,'47')
      j = index(string,'47',.true.)
      print*,i,j   !! 5 and 17

      i = index(string,'4711')
      j = index(string,'4711',.true.)
      print*,i,j   !! both 17
      end


In the first part index searches for "47" which appears three times - at position 5, 11 and 17. In the second part "4711" is searched which appears only once.
Back to top
View user's profile Send private message
Kenneth_Smith



Joined: 18 May 2012
Posts: 814
Location: Hamilton, Lanarkshire, Scotland.

PostPosted: Fri Feb 27, 2015 7:15 am    Post subject: Reply with quote

Thanks guys, I missed the point that the position is always counted from the left. Obvious really! Thanks Ken
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group