Copyright A. Pywell.
Another Last Word On INSTR:
We should by now be able to write some code to find a string within a string. (the experts call the little string a ‘substring’). But , having found the substring, what if we want to find out if the substring occurs again?. Easy-peasy!. Our friendly neighbourhood QL allows you to search just part of a string and you can specify which part.
Remember POSITION = “PYW” INSTR “ALAN PYWELL”?. POSITION takes a value of 6. To save my typing fingers (one on each hand) let’s assume that we have previously said A$ = “ALAN PYWELL”. So POSITION = “PYW” INSTR A$ also returns 6 . Note that the keyword LET is optional, so I save my fingers again. Incidentally, I suppose that you , like me, read “Position equals ‘PYW’ Instring etc.. NAUGHTY!. You (and I) should read ‘(Let) Position take the value of’. I wonder if anyone other than authors reads it correctly?.
But suppose for some reason our prog says POSITION = INSTR A$ (6 TO LEN (A$)) ?. This is quite legitimate. What, do you suppose, is the value of POSITION now?. Well, the string (substring!) we were searching is “PYWELL” so INSTR found a match on the first letter. POSITION therefore takes the value 1. To put it another way, if you specify where the search is to begin then POSITION will start counting from that position and the count begins at 1 – the earlier part of the string to be searched is ignored. I’ve rambled on about this because if you want to carry on a search after finding one match you need to be aware of all that’s just been said.
Let us suppose that A$ = “Alan Pywell is a towering genius and modest with it. Alan Pywell is also a liar”.
POSITION = “PYW” INSTR A$ will again return a value of 6 in POSITION. But we want to continue the search but we do not want to perform the pointless exercise of searching from the start of the string, so this is where the facility to specify which part of a string we want to search. Neither can we resume the search from POSITION because the first occurrence of “PYW” would be found immediately. We must resume the search from at least one character further on i.e. from POSITION + 1. Let’s try POSITION = “PYW” INSTR A$ (POSITION + 1 TO LEN (A$)). Counting starts from the “e” in the first Pywell – when it finds the next occurrence of Pywell it will have counted to 51. But this tells us only that the second Pyw is 51 characters beyond the “e” in the first Pywell – not a lot of use. What we must do is tell QL to add the count of the first search to the count of the second but Oh, dear, silly old QL has forgotten the first count!.
So let’s tell QL to remember the first count!.
POSITION = "Pyw" INSTR A$
OLD_POSITION = POSITION: REMark QL 'remembers' the first count
POSITION = "Pyw" INSTR A$(OLD_POSITION + 1 TO LEN (A$))
POSITION_OF_SECOND_OCCURRENCE = POSITION + OLD_POSITION
Now OLD_POSITION + POSITION (6 + 51) = 57. I’m a great believer in making people think so I have to tell you that the second occurrence of “Pyw” is the fiftyeigth character. So the last line of the prog above should read:
POSITION_OF_SECOND_OCCURRENCE = OLD_POSITION + POSITION + what?
Oh, alright, here’s a clue. Add one. Experiment a bit with your own string and substring – you’ll soon get it sorted out in your mind.
(Thinks ‘Oh dear, here comes the dreaded GOTO again’
After my remark about fools and GOTO I received a couple of vitriolic letters from the anti-GOTO brigade. One writer claimed that his intelligence puts him in “the top 1% of the population” . If I had dignified his tirade with a reply I would have said that so does mine and greater intelligence gives greater capacity for being a fool. I don’t advocate the liberal sprinkling of progs with GOTO but I do believe that the occassional use of this extremely useful keyword helps the learner no end. Look at this:
1000 PRINT "PRESS SPACE TO CONTINUE"
1010 KEY$ = INKEY$
1020 IF KEY$ <> " " THEN GOTO 1010:REMark Loops back until space is pressed.
Nothing wrong with that I say – it works!.
The above writer went on to say that if one’s car has an ashtray then one should take up smoking, ‘because it’s there’, according to my argument!. I didn’t expect sense from one so blinkered. GOTO is there, but I never argued that one must use it- simply that one can use it if one wishes. I frequently use a GOTO when programming to simplify my thought processes. At some stage I make the programme ‘more elegant’ by replacing many, but not necessarily all, GOTOs.
I would welcome suggestions for Part Five, I love receiving letters
Alan Pywell [address omitted]
The above article was received too late to go into our November issue because Alan has been in and out of hospital in September/October resulting in him not being able to get the disk in the mail in time.
You will note that Alan is asking for what YOU would like him to write about, so why not drop him a line and say something – even although it is just – “get well soon”, I think that would be nice. Don’t you?