Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29275 Discussions

Problems with using INDEX(substr,str) function in Intel Visual Fortran 2011 running on MSVS 2008 Professional

Gary_E_
Beginner
1,314 Views


This seems like a simple function to use but I am having problems with the INDEX(substr,str) function returning a zero (0) value even when the substring is within the string.

For example:

Comma delimiter between text input

CHARACTER(LEN=1024) :: STR

STR='ARG1=40,ARG2=2,ARG3=13.5,ARG4=29.6,ARG5=2.9,ARG6=1,*                                             ... ... ...'

LOCC=LEN_TRIM(STR)

LOCD=INDEX(',',STR(1:LOCC)) ! ',' is delimiter for all individual keywords

In the case above, LOCD=0 after the INDEX() statement. I get the same result (LOCD=0) if I use INDEX(',',STR) or if I save ',' to a character variable and use that in the substring argument.

What am I missing here??

IDE information:

Intel(R) Visual Fortran Composer XE 2011 Update 7 Integration for Microsoft Visual Studio* 2008, 12.1.3518.2008, Copyright (C) 2002-2011 Intel Corporation

Intel® C++ Composer XE 2011 Update 10 Integration for Microsoft Visual Studio* 2008, Version 12.1.1111.2008, Copyright© 2002-2012 Intel Corporation

Microsoft Visual C++ 2008

0 Kudos
4 Replies
Gary_E_
Beginner
1,314 Views

Update: I decided to try the SCAN(STRING,SET,BACK) function in Fortran and this worked perfectly! LOCD is now set to the value of 9, which is the first instance of the character ',' in STR.

INDEX() works just fine in another case where the substring is set to the '*' character. INDEX() also barfed when the substring was set to the '&' character. Is there something about these particular characters that Fortran doesn't like??

0 Kudos
bmchenry
New Contributor II
1,314 Views

I think your use od INDEX had the arguments reversed. You used LOCD=INDEX(',',STR(1:LOCC)) ! ',' whereas the proper use is

result = INDEX (string, substring)

so STR(1:LOCC) should have been first

0 Kudos
mecej4
Honored Contributor III
1,314 Views

Usage in Fortran: INDEX(string,substring,...). Did you have the wrong order for the first two arguments?

0 Kudos
Gary_E_
Beginner
1,314 Views

Hahaha! Yes, thanks. I figured that out since my last post. The reference I was using for INDEX() had the argument order mixed up, showing INDEX(substring,string). If there was a bashful smiley I'd post that here.

0 Kudos
Reply