DAY 032: _INSTRREV - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: Official Links (https://qb64phoenix.com/forum/forumdisplay.php?fid=16) +--- Forum: Learning Resources and Archives (https://qb64phoenix.com/forum/forumdisplay.php?fid=13) +---- Forum: Keyword of the Day! (https://qb64phoenix.com/forum/forumdisplay.php?fid=49) +---- Thread: DAY 032: _INSTRREV (/showthread.php?tid=1269) Pages:
1
2
|
DAY 032: _INSTRREV - Pete - 12-12-2022 _INSTRREV is an INSTR function, but in reverse! Instead of searching left to right, it searches from right to left. SYNTAX _INSTRREV(seed%, string$, search$) Where: seed% is starting point in the string to begin the search. string$ is the string to be searched. search$ is the search term. Note: The first parameter, seed%, is optional. We can also code it as: _INSTRREV(string$, search$) Use: Parsing function. So if we can already search a string forward with INSTR(), what's the benefit of using _INSTRREV()? Glad you asked... Let's say we have a page margin of 40 spaces wide. Here is our first string for that document... a$ = "Pete and Steve walk into a bar. Steve blames Pete for not raising the bar higher." Oops, that's longer than 40-characters? What do we do now Batman? Well, simple "Stevey-boy" Wonder, we use the Bat-o-axe and chop it! (I have an old bat-o-axe around the house... Ouch! Hit by the bat-pan again!) Well while I recover from being being badly bat-tered, let's take a short commercial break and see INSTR() vs _INSTRREV() in action. Code: (Select All) WIDTH 82, 25 Now the seed% part in _INSTRREV is used a bit differently than INSTR() in that it is read right to left, instead of left to right. So instead of stating your seed% at zero, you start it at the last space - 1 in your string to be chopped. Code: (Select All) a$ = "Pete and Steve walk into a bar. Steve bl" Well one practical application I wish we could do with this keyword is seed it find a term in a list of terms, separated with a delimiter. Well, we can build a function and use our key INSTRREV() and I'll throw in _INSTR() for no extra charge. First input if you are searching forwards or backwards and then input the what number term to find, 1 - 10. Code: (Select All) DO Other uses are parsing off a file path to show just the directory: From the Wiki... Code: (Select All) fullPath$ = "C:\Documents and Settings\Administrator\Desktop\qb64\internal\c\libqb\os\win\libqb_1_2_000000000000.o" One last thing, which also applies to INSTR(). Both will return zero if the search term, or aka sub-string, isn't found. So if you are using it with mid$() be aware that zero will give you the full string in the output, instead of a null string. You will need to write a conditional statement to handle this behavior... Code: (Select All) a$ = "123456789" ' No space(s). Tune in tomorrow. Same Bat time, same Bat channel. Pete RE: DAY 032: _INSTRREV - vince - 12-12-2022 Nice guide, Pete. So reversing a string and returning LEN(s$) - INSTR(s$, ...) is too much of an exercise that it warrants linguistic enhancement and a whole page but something more nuanced and conducive to optimization like a profound INPUT$ warrants a thesis on the pedagogy of doing it yourself RE: DAY 032: _INSTRREV - mnrvovrfc - 12-12-2022 This was badly needed in QuickBASIC, although many more years would pass before there were enough users of Linux that searching from the end of a string to the beginning for a "slosh" became necessary. I have yet to see in somebody else's source code this function replicated by reversing a string, using "INSTR" and then creating a return integer offset. OTOH reversing a string required some performance penalty. I had thought "FilenamePart" function in Purebasic was a "black box". (It returned only the eight characters or less of an "8-dot-3" MS-DOS-style filename, or the similar portion between the last "slosh" and the period that separates it from the suffix.) But what it did, wound up being used in a lot of my programs for that system. I worked on an emulation of that function for QB64. I wound up doing a "stringfieldseg" which does the "_INSTRREV" thing for either slash and then returns the rest of the string which is assumed to be a filename. EDIT: Got ninja'ed by vince, and he was able to tell before me what I said in the second paragraph. :tu: RE: DAY 032: _INSTRREV - vince - 12-12-2022 I need help getting this to work Code: (Select All) deflng a-z RE: DAY 032: _INSTRREV - Pete - 12-12-2022 String = "fi neht evetS lluf retfa ro og evetS hcum" SubString$ = "evetS" You have two different instances of the SubString, so is your goal to count from left to right or right to left? Well, I would look at it this way... Code: (Select All) $CONSOLE:ONLY Pete RE: DAY 032: _INSTRREV - vince - 12-13-2022 don't hurt yourself, Pete, I was just trying to recreate _INSTRREV in QB45. Goes to show we're no Steve to be messing with this stuff, I'm glad it was added into PE RE: DAY 032: _INSTRREV - Pete - 12-13-2022 Oh, that's what you were shooting for. Okay, maybe you were just over-thinking it? Code: (Select All) a$ = "much Steve go or after full Steve then if" I get the two Steve's now. It needs to pick out the second one to be _INSTRREV() compliant. Good thinking! Pete RE: DAY 032: _INSTRREV - vince - 12-13-2022 beautiful code, reminiscent of a young Steve RE: DAY 032: _INSTRREV - SMcNeill - 12-13-2022 (12-13-2022, 02:02 AM)vince Wrote: beautiful code, reminiscent of a young Steve Did someone slip some viagra, or something, to vince? Man, he's really been obsessing over Steve for a while now! RE: DAY 032: _INSTRREV - SMcNeill - 12-13-2022 Here's about the simplest and easiest example I can imagine for use with _INSTRREV: Code: (Select All) a$ = "This is a string of text for testing." Start at the end of your text, work backwards, until you're at the front of your text. It's just that simple. If you can use INSTR, then you shouldn't have any issues using INSTRREV. |