like function

General FreeBASIC programming questions.
Gonzo
Posts: 722
Joined: Dec 11, 2005 22:46

like function

Postby Gonzo » Jan 09, 2012 20:56

does anyone have a like function thats close or more advanced than vb6's version?
i have a database course this semester, and i need something to do, so i intend to write a database server
out of interest, i want to see what others have already made that can parse regular expressions, or something similar

i will probably write my own simple like function just like VB6..
but since i rarely do any string parsing i dont know any of the neat tricks to iterating them (and dealing with characters) =)
Merick
Posts: 1038
Joined: May 28, 2007 1:52

Re: like function

Postby Merick » Jan 10, 2012 0:13

I think the closest FB equivalent is "instr"

Since the FB wiki seems to be down atm I can't post a link to it, so I'll post this from the fb help file instead:

InStr Locates the first occurrence of a substring or character within a string


Syntax

Declare Function InStr Overload ( ByRef str As Const String, [ Any ] ByRef substring As Const String ) As Integer
Declare Function InStr ( ByRef str As Const WString, [ Any ] ByRef substring As Const WString ) As Integer
Declare Function InStr ( ByVal start As Integer, ByRef str As Const String, [ Any ] ByRef substring As Const String ) As Integer
Declare Function InStr ( ByVal start As Integer, ByRef str As Const WString, [ Any ] ByRef substring As Const WString ) As Integer


Usage

first = InStr( [ start, ] str, [ Any ] substring )


Parameters

str

The string to be searched.

substring

The substring to find.

start

The position in str at which the search will begin.


Return Value

The position of the first occurrence of substring in str.


Description

Locates the position of the first occurrence of a substring or character within a string. In the first form of InStr, the search begins at the first character.

Zero (0) is returned if substring is not found, either str or substring are empty strings, or start < 1.

If the Any keyword is specified, InStr returns the first occurrence of any character in substring.


Example


' It will return 4
Print InStr("abcdefg", "de")

' It will return 0
Print InStr("abcdefg", "h")

' It will search for any of the characters "f", "b", "c", and return 2 as "b" is encountered first
Print InStr("abcdefg", Any "fbc")



Dim test As String
Dim idx As Integer

test = "abababab"
idx = InStr(test, "b")

Do While idx > 0 'if not found loop will be skipped
Print """b"" at " & idx
idx = InStr(idx + 1, Test, "b")
Loop
counting_pine
Site Admin
Posts: 6180
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: like function

Postby counting_pine » Jan 10, 2012 15:13

www.freebasic.net/wiki/wikka.php?wakka=keypgInstr
It takes a few goes, but you can usually get there in the end if you refresh enough times. It helps to "enter" the URL manually which prevents it passing the referrer page.

Failing that: www.google.com/search?q=cache:freebasic ... KeyPgInstr
Sisophon2001
Posts: 1704
Joined: May 27, 2005 6:34
Location: Cambodia, Thailand, Lao, Ireland etc.
Contact:

Re: like function

Postby Sisophon2001 » Jan 13, 2012 13:10

I don't know the VB like function, but you may be interested in the code samples below, which I use for searching for string matches.

Garvan

Code: Select all

''------------------------------------------------------------------------------
''
'' Function    : like()
''
'' Purpose     : matches wild cards * and ?
''
'' Source      : Converted from C source code of BCX (LGPL licence)
''
'' History     : Date       Reason
''               16-07-06   Created
''
''------------------------------------------------------------------------------
function like(byval main as zstring ptr, byval pat as zstring ptr) as integer
   dim as ubyte a, b, d
   dim as ubyte ptr r, p
   dim as integer star
   
   do
      d = *pat
      pat+=1
      if d = 0 then
         return (star <> 0) or (*cptr(ubyte ptr,main) = 0)
      elseif d = asc("*") then
         star = 1
      elseif d = asc("?") then
         if *cptr(ubyte ptr, main) = 0 then return 0
         main+=1
      else
         exit do
      end if
   loop

   b = d
   do
      a = *main
      main+=1
      if a = b then
         r = main
         p = pat
         do
            d = *p
            p+=1
            if d = asc("*") then
               if like(r,p-1) = -1 then return -1
               exit do
            elseif d = 0 then
               if *r = 0 then return -1
               exit do
            end if
            r+=1
         loop while (*(r-1) = d) or (d = asc("?"))
      end if
   loop while star <> 0 and a <> 0
   return 0
end function

''------------------------------------------------------------------------------
''
'' Function    : isoundex()
''
'' Purpose     : matches words that sound similar
''
'' Source      : Basic techniques and Utilities by Ethan Winer
''
'' History     : Date       Reason
''               16-07-06   Created
''
''------------------------------------------------------------------------------
function isoundex(word as string) as integer
   dim as string work
   dim as integer wkpos, prevcode, temp, l
   
   work = "0000"
   wkpos = 1
   prevcode = 0
   
   for l = 1 to len(word)
      temp = instr("bfpvcgjkqsxzdtlmnr", mid(word, l, 1))
      if temp then
         temp = asc(mid("111122222222334556", temp, 1))
         if temp <> prevcode then
            mid(work,wkpos) = chr(temp)
            prevcode = temp
            wkpos += 1
            if wkpos > 4 then exit for
         end if
      else
         prevcode = 0
      end if
   next
   return val(work)
end function
Gonzo
Posts: 722
Joined: Dec 11, 2005 22:46

Re: like function

Postby Gonzo » Jan 13, 2012 13:17

looks good, though i will probably extend the like to include # and 0 =)
thanks!
not sure how you determine words that sound like others, but definitely interesting
rdc
Posts: 1713
Joined: May 27, 2005 17:22
Location: Texas, USA
Contact:

Re: like function

Postby rdc » Jan 13, 2012 14:02

Garvan is using the soundex function which uses a phonetic algorithm to match words that "sound" alike. I used it a lot when writing client-server applications back when I programmed for a living.

Return to “General”

Who is online

Users browsing this forum: MSN [Bot], SARG and 2 guests