indexing - VB6: Grabbing String Starting with x From an Array -


let's have following array, arr contains maximum of 20 substrings. in example, array have 4 substrings:

arr = {"aaaa: 1234567" , "bbbb: 2345678" , "cccc: 98765432" , "dddd: 87654321"}

note: capitalization not important.

i needing assistance finding substring inside array (again, 20 strings possible), starts with cccc:, , assigning entire string it's own variable, let's call var1.

i understand assign var1 = arr(2), need figure out how determine index number in array meets criteria.

what really appreciate (although method happy with), making stand alone function such as:

function arrayindex(arrayinput variant, startswith string) byte    'arguments here end function 

then use in subroutine:

var1 = arr(arrayindex(arr, "cccc:")) 

update 1

here snippet of current code

sub t_report     '<shortcut = shift+ctrl+t>      dim data string, dataarray variant     activesession         .copy 0, 2, 80, 21  'just big block of text multiple lines, copied clipboard         data = clipboard    'set data clipboard value         dataarray = split(data,vbcrlf)  'this "data" in array, separated line breaks          'just checking see if array successful (it is)          debug.print dataarray(0) & vbnl & dataarray(1) & vbnl & dataarray(2) & _             vbnl & dataarray(3) & vbnl & dataarray(4) & vbnl & vbnl           'misc code here           dim sf1 string, sf2 string, sf3 string         dim rslt1 string, rslt2 string, rslt3 string         sf1 = "field1:"         sf2 = "field2:"         sf3 = "field3:"          msgbox dataarray(0)     ' works fine, giving me first substring         rslt1 = findmystring(sf1, dataarray)          ' misc code      end end sub 

however, when use following function, type mismatch error on msgbox arr(0) line, although should giving me first substring of dataarray in above sub (should exact match, array has not been modified).. however, when msgbox dataarray(0) above, do first substring.

private function findmystring(strtofind string, paramarray arr() variant) string      dim integer     dim ilen integer     dim strarr string      findmystring = ""   ' returns blank string if not found      ' type mismatch here (doesn't appear array sub loaded function)     msgbox arr(0)         ilen = len(strtofind)     = 0 ubound(arr)     strarr = cstr(arr(i))         if strtofind = left$(strarr, ilen)             findmystring = strarr             exit function         end if     next  end function 

edit - fix array load

change (it must give error!):

arr = {"aaaa: 1234567" , "bbbb: 2345678" , "cccc: 98765432" , "dddd: 87654321"} 

to this:

dim arr variant arr = split("aaaa: 1234567,bbbb: 2345678,cccc: 98765432,dddd: 87654321", ",") 

if want use function bidding can pass array of data using paramarray type. has last parameter in function

this should want:

private function findmystring(strtofind string, paramarray arr() variant) string     dim integer    dim ilen integer    dim strarr string     findmystring = "" ' returns blank string if not found '     debug.print arr(0)(0) ' print first element of array     ilen = len(strtofind)    = 0 ubound(arr(0))       strarr = cstr(arr(0)(i))       if strtofind = left$(strarr, ilen)           findmystring = strarr           exit function       end if    next  end function 

in example can test by:

var1 = findmystring("cccc:", arr) 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -