Skip to main content
Solved

BASIC INDEX function does not appear to be working correctly

  • March 30, 2026
  • 5 replies
  • 47 views

Mark Warner

in Basic.  I have a program which calls a RESTApi and returns results and puts it into a txt file.   When I read the test file with another basic program, an INDEX funtion that I need to perform doesn't appear to work (returns zero).  When I put the file contents into a basic variable, the INDEX function works correctly.   I’ve looked at the file for special or non-printable characters which might cause the INDEX function not to work, and the file itself is clean.  What if anything else can I try  (and I’ve tried a bunch)..

Best answer by Tyrel Marak

Mark,

I think it’s the .txt files you're consuming.  When I look at it, each pair of normal printable characters is separated by a null character.  All of the new-lines turn into field-marks and the file starts with a char(255) and a char(254). So it looks like there are 8 attributes in the .txt.  You probably need to massage it a bit before trying to process.

I played with it a bit. If you do the following, I think it should work as you want:
CONVERT CHAR(0) TO ‘’ IN RESPREC

CONVERT CHAR(13) TO @AM IN RESPREC

RESPREC = TRIM(RESPREC,@AM,’R’)

 

5 replies

Jonathan Smith
Forum|alt.badge.img+4

Mark, could you put together some basic code that demonstrates the failure

Thanks,


Tyrel Marak
Forum|alt.badge.img+1
  • Participating Frequently
  • March 30, 2026

Hi Mark. The syntax is
INDEX(string, substring, occurrence)
and the help/documentation says: “If the specified occurrence of the substring is not found, or if string or substring evaluate to the null value, 0 is returned.”  So have you debugged to determine whether or not the string and substring that you’re feeding the function are non-null?


Mark Warner
  • Author
  • New Participant
  • March 30, 2026

Tyrel,  yes I have debugged that.   When I assign variables inside the program with the contents of the file (i.e.  line1 = rec<1>  line2 = rec<2> and so on), the INDEX works correctly.  

The programs are as follows:  (please excuse the coding style, these were quick and dirties)

The TAXAPITEST program reads from the file in which the API creates from the response record (it’s all text)

The TESTAPITEST-NOREAD is the same program but the contents of the file are assigned to internal variables


Tyrel Marak
Forum|alt.badge.img+1
  • Participating Frequently
  • Answer
  • March 30, 2026

Mark,

I think it’s the .txt files you're consuming.  When I look at it, each pair of normal printable characters is separated by a null character.  All of the new-lines turn into field-marks and the file starts with a char(255) and a char(254). So it looks like there are 8 attributes in the .txt.  You probably need to massage it a bit before trying to process.

I played with it a bit. If you do the following, I think it should work as you want:
CONVERT CHAR(0) TO ‘’ IN RESPREC

CONVERT CHAR(13) TO @AM IN RESPREC

RESPREC = TRIM(RESPREC,@AM,’R’)

 


Mark Warner
  • Author
  • New Participant
  • March 30, 2026

Thanks Tyrel..  That appears to have done the trick..  I appreciate you looking and figuring this out…