find is like Instr, what's like RevInstr?

Q&A's, tips, howto's
Locked
axtens
Posts: 28
Joined: Mon Apr 06, 2009 12:23 pm
Location: Perth, WA Australia
Contact:

find is like Instr, what's like RevInstr?

Post by axtens »

G'day everyone

(find) is like VBScript's Instr(). What is the equivalent for VBScript's RevInstr() wherein one searches for the first occurrence from the other end of the string? (find (reverse))?

Kind regards,
Bruce.

axtens
Posts: 28
Joined: Mon Apr 06, 2009 12:23 pm
Location: Perth, WA Australia
Contact:

Re: find is like Instr, what's like RevInstr?

Post by axtens »

axtens wrote:What is the equivalent for VBScript's RevInstr() wherein one searches for the first occurrence from the other end of the string? (find (reverse))?
I think I'll have a go at answering my own question.

Code: Select all

(set 'reverse-find 
	(lambda (findThis inThis) 
		(- (length inThis) (find (reverse findThis) (reverse inThis)) (length findThis))))
and then some demonstrations plus a comparison of what it finds against what (find) finds.

Code: Select all

(set 'toFind "dog")
(set 'sentence "My first dog's fleas are on my second dog's blanket")

(println (slice sentence (reverse-find toFind sentence) (length toFind)))
; "dog"
(println (slice sentence (reverse-find toFind sentence)))
; "dog's blanket"
(println (slice sentence (find toFind sentence)))
; "dog's fleas are on my second dog's blanket"
Am I doing okay so far?

Kind regards,
Bruce.

Kazimir Majorinc
Posts: 388
Joined: Thu May 08, 2008 1:24 am
Location: Croatia
Contact:

Post by Kazimir Majorinc »

I don't know VBscript but it looks OK to me.

I think you are progressing quite good. Are you experienced programmer?

axtens
Posts: 28
Joined: Mon Apr 06, 2009 12:23 pm
Location: Perth, WA Australia
Contact:

Post by axtens »

Kazimir Majorinc wrote:I think you are progressing quite good. Are you experienced programmer?
Yes, just not in Lisp. I've been in the IT business in various ways since 1983. At the moment I'm working for The Protium Project and MyTongue as a Software/Test Engineer. I program in a variety of languages, including Ada, assembler, (Visual) BASIC, C/C++, Delphi, Fortran, Perl, Protium, Tcl, and VBScript (and a few lesser known languages.)

Kind regards,
Bruce.

Locked