Page 1 of 1 1
Topic Options
#193980 - 09/05/20 11:55 PM need help with creating dynamic arrays.
Dan2413 Offline
Just in Town

Registered: 09/05/20
Posts: 1
Loc: VA
I'm trying to force users to select one of a long list of items. (Actually user and Empl ID number.) This is to populate a field in a CSV file that is created by the script. The CSV file, in turn is used to feed a system over which we have no control.

I'm able to parse a file containing the user data into an array. no problem. I'm also able to fill a variable one keystroke at a time. again, not that big a deal. What I'd like to do is conduct a partial match search of the 'master' array from left to right and, when the resultant match set is less than say 10 items, present them as choices to the user to select from.

Seeking the first 'match' (only the leftmost character) is not so bad, but since I can't determine in advance how many matches there will be for the first TWO (then three, four...) characters, how do I initially declare the search result array? I'd think it has to be a size [0] and add as matches are discovered. That way I'd be assured that the size of the resultant array is the number of matches.


OK.. I K.N.O.W this is not an efficient way of doing this, so I'm open to suggestions.

Top
#193985 - 09/05/21 09:54 AM Re: need help with creating dynamic arrays. [Re: Dan2413]
Richard H. Administrator Offline
Administrator
*****

Registered: 00/01/24
Posts: 4940
Loc: Leatherhead, Surrey, UK
You don't need two arrays.

  • Create a master array.
  • Sort it.
  • As the characters are typed you just need to record the first element and the count of matches
  • Using the start and the count you can pull out the information as you wish.


Because the list is sorted and you are doing simple prefix match this will work well. If you were using pattern matching or searching anywhere in the string you would have to construct an extract array.

Top
#193987 - 09/05/21 12:09 PM Re: need help with creating dynamic arrays. [Re: Richard H.]
Richard H. Administrator Offline
Administrator
*****

Registered: 00/01/24
Posts: 4940
Loc: Leatherhead, Surrey, UK
Here is an example using a pre-sorted wordlist:
 Code:
CLS
Dim $sMaster,$sOldInput,$sInput,$c,$iMaxDisplay,$i

$iMaxDisplay=5

$sInput=""

$sMaster=Split("ABLE ABOVE ACCESS ACHIEVED ACTUAL ADAPTIVE AFTER AGAIN ALGORITHM ALL ALLOW ALLOWS ALLY"
	+" ALMOST ALREADY ALSO ALWAYS AMOUNT AN AND ANOTHER ANY APPLIES APPROVED ARCHIVE"
	+" ARCHIVER ARE AROUND AS ASCII ASSUME ASSUMED ASSUMES AT ATARI ATTEMPT ATTEMPTS AUTOMATIC"
	+" AVOID AVOIDED B BACKGROUND BAD BE BECAUSE BEEN BEFORE BEGINS BEHAVE BEST BETTER"
	+" BIASED BIT BITS BLOCK BLOCKCOMPRESS BLOCKS BOUNDARY BUT BY BYTES C CAN CASE CASES CAT"
	+" CATED CC CD [CDFHLLNNRTVV] CHANGE CHANGED CHARACTERS CHECK CHECKS CODE CODING"
	+" COM COMBINATION COMMAND COMMON COMPACT COMPILATION COMPLAIN COMPLEMENT COMPRES COMPRESS"
	+" COMPRESSED COMPRESSES COMPRESSING COMPRESSION CON CONCATENATE CONCATENATED"
	+" CONDITIONS CONFLICT CONSISTENCY CONSISTS CONSTRUCTED CONVENTIONS CONVERSION CONVERT"
	+" CONVERTED COPIES COPY COPYING COPYRIGHT CORRECT CORRESPONDING CORRUPT CORRUPTED"
	+" COULD CR CRC CREATE CREATED CSH CURRENTLY D DAMAGE DAMAGED DARD DATA DATE DEAL DECOMPRESS"
	+" DECOMPRESSED DECOMPRESSING DECOMPRESSION DEFAULT DEFLATE DEFLATION DELIMITED"
	+" DEPENDS DERIVED DESCEND DESIGNED DETECT DETECTED DETECTION DETECTS DEUTSCH DEVICE DEVRST"
	+" DIGIT DIRECTORIES DIRECTORY DIS DISCOVER DISK DISPLAY DISPLAYED DISTRIBUTE"
	+" DISTRIBUTED DO DOES DOTS EACH EG EITHER EMITS ENDOFLINES ENDS ENGLISH ENTIRE ENVIRONMENT"
	+" EQUIVALENT ERROR EVEN EVERY EXAMPLE EXCEPT EXISTING EXISTS EXIT EXPAND EXPANSION"
	+" EXPENSE EXPLICIT EXPORT EXTENSION EXTENSIONS EXTRA EXTRACT EXTRACTED FAILURE FASTEST FAT"
	+" FEATURE FEW FIELDS FIFO FILE [FILE FILEGZ FILES FINDS FIRST FLAG FOLLOWING"
	+" FOO FOR FORCE FORCES FORM FORMAT FORMATS FOUNDATION FREE FROM FUSION GAILLY GARBAGE GB GENER"
	+" GENERALLY GENERATES GET GIVEN GIVES GNU GRANTED GUNZIP GZ GZCAT GZEXE"
	+" GZIP GZIPOPT GZIPQ GZIPV H HAD HANDLE HAPPILY HAS HAVE HEADER HELP HH HHEELLPP HIGH HIGHLY"
	+" HOLD HOWEVER HUFFMAN IDENTICAL IF IFICATION IGNORE IGNORED IMPLICITLY IN INC"
	+" INCLUDE INCORRECT INCREASES INDEPENDENTLY INDICATES INFORMATION INPUT INSTALLED INSTEAD"
	+" INTEGRITY INTENDED INTERNET INTERPRETED INTO INVALID INVOCATION INVOKE INVOKED"
	+" IS IT ITS JEANLOUP K KEEP KEEPING KEEPS LANGUAGE LARGE LARGER LAST LATER LATION LEFT LEGAL"
	+" LEMPELZIV LENGTH LESS LET LEVEL LF LICENSE LIMIT LIMITED LINE LINES LINK"
	+" LINKS LIST LL LLIICCEENNSSEE LLIISSTT LN LOCAL LONG LONGER LONGEST LOST LZ LZH LZW MACHINE"
	+" MAGIC MAKE MAN MANUAL MAT MAY MEANS MEDIA MEMBER MEMBERS MEMORY METHOD"
	+" METHODS MISSION MOD MODE MODES MODIFIED MODULO MORE MSDOS MUCH MUL MULTI MULTIPLE N NAME NAMED"
	+" NAMES NECESSARY NEED NEVER NEW NEWGZ NN NNAAMMEE NNOONNAAMMEE"
	+" NNOOTTIICCEE NO NON NONUNIX NORMALLY NOT NOTE NOTICE NT NULL NUMBER OBTAIN OBTAINED OCCURS"
	+" OF OLDGZ ON ONCE ONE ONLY OPTION OPTIONS OR ORIGINAL OS OTHER OUPUT OUTPUT"
	+" OVERWRITE OVERWRITTEN OWNERSHIP P PACK PAD PARAMETERS PART PARTIC PARTS PASSED PER PERCENTAGE"
	+" PERFORMED PERMISSION PKZIP PLE PLUS POINT POSSIBLE PRESENT PRESERVE"
	+" PRESERVED PRESERVES PRESS PRESSED PRESSION PREVIOUS PROBLEM PROGRAM PROGRAMSSEEEE PROMPTS"
	+" PROVIDED Q QQ QQUUIIEETT QUIET QUIT RARE RATIO RATIOS READ READING RECOGNIZED"
	+" RECOGNIZES RECOMPRESS RECOVER RECOVERED RECURSIVELY REDUCED REDUCES REDUCTION REDUNDANT"
	+" REGARDLESS REGULAR REGULATE RELEVANT REMOVE REMOVED RENAME REPLACED"
	+" REPLACES REPORTED REPORTS REPRESENTS RESPECTIVELY RESPOND RESTORE RESTORED RESULTING RFC RR"
	+" RUNNING S [S SAME SAVE SAVED SCO SCREEN SEE SEEKABLE SEQUENCE SET SETENV"
	+" SEVERAL SH SHORTHANDS SHOULD SIGSEGV SIMPLY SINGLE SION SISTENCY SIZE SIZES SLIGHTLY SLOWEST"
	+" SMALL SO SOCKET SOFTWARE SOME SOMETIMES SOURCE SPECIAL SPECIFICATION"
	+" SPECIFIED SPEED SS [SS SSTTDDOOUUTT SSUUFF SSUUFFFFIIXX SSUUFFFFIIXX] STAMP STAN STANDARD STATED"
	+" STATUS STDOUT STILL STORED STRUCTURE SUBSTRINGS SUCH SUF SUFFIX"
	+" SUFFIX] SUFFIXES SUITABLE SUPPORTED SUPPORTS SUPPRESS SURE SYMBOL SYMBOLIC SYSTEM SYSTEMS"
	+" TAKES TAPE TAPES TAR TARBBUUGGSS TARGZ TARZIP TERED TERMINAL TERMS TEST"
	+" TEXT THAN THAT THE THEIR THEM THEN THERE THESE THEY THIS THOSE TIME TIMES TIMESTAMP TIMESTAMPS"
	+" TIPLYLINKED TITLE TO TOO TOTALS TOWARDS TRAILING TRANS TRANSFER TRANSFERRED"
	+" TRANSLATIONS TRANSPARENTLY TRAVEL TRIBUTION TRUE TRUN TRUNCATE TRUNCATED TRUNCATES TRUNCATING"
	+" TRY TT TTAARR TTAARRGGZZ TTAARRZZ TTAAZZ TTEESSTT TTGGZZ TTOOSSTTDDOOUUTT"
	+" TWO TYPICALLY UAL ULAR UNAL UNCHANGED UNCOM UNCOMPRESS UNCOMPRESSED UNCOMPRESSEDNAME UNCOMPRESSES"
	+" UNCOMPRESSING UNDER UNKNOWN UNLESS UNZIP UP USAGE USE USED USEFUL USES"
	+" USING USUALLY VARIABLE VAXVMS VERBATIM VERBOSE VERIFY VERSION VERSIONS VIOLATION VMS WANT WARNING"
	+" WARNINGS WAS WC WERE WHEN WHENEVER WHERE WHETHER WHICH WHILE WHOLE WHOSE"
	+" WILL WINDOWS WISH WITH WITHIN WITHOUT WORK WORSE WORST WRITE WRITES WRITING WRITTEN XFZ"
	)


At(1,5) "Maximum entries to display: "+$iMaxDisplay+" words in list: "+(UBound($sMaster)+1)
AT(4,5) "  (Use the ESCAPE key to exit)"
AT(3,5) "Enter a substring to search on: "
While "Bears Poo In The Woods"
	Get $c
	Select
	Case ASC($c)=27
		CLS Exit 0
	Case ASC($c)=8
		If $sInput $sInput=Left($sInput,Len($sInput)-1) EndIf
	Case $c >="A" AND $c <="z"
		$sInput=$sInput+$c
	Case "Not a valid character"
		BEEP
	EndSelect
	If $sInput<>$sOldInput
		At(3,37) Color g+/n $sInput+" " Color w/n At(3,37) Color g+/n $sInput Color w/n
		$sOldInput=$sInput
		At(9,5) "Working...                                      " At(5,15)
		$iMatches=0
		$iMatchStart=-1
		If $sInput<>""
			$iMatchStart=UBound(Split(Left(" "+Join($sMaster),InStr(" "+Join($sMaster)," "+$sInput))))
		EndIf
		If $iMatchStart>0
			$iMatchStart=$iMatchStart-1
			While $iMatches+$iMatchStart<UBound($sMaster) AND InStr($sMaster[$iMatches+$iMatchStart],$sInput)=1
				$iMatches=$iMatches+1
			Loop
		EndIf
		At(9,5) $iMatches " matches starting at "+$iMatchStart
		For $i=1 To $iMaxDisplay
			At(10+$i,10) "                                                "
		Next
		If $iMatches<=$iMaxDisplay
			For $i=1 To $iMatches
				At(10+$i,10) Right("   "+CStr($i),3)+":  "+$sMaster[$iMatchStart+$i-1]
			Next
		EndIf
		At(3,37) Color g+/n $sInput+" " Color w/n At(3,37) Color g+/n $sInput Color w/n
	EndIf
Loop

Top
#193989 - 09/05/21 09:25 PM Re: need help with creating dynamic arrays. [Re: Richard H.]
Gargoyle Offline
MM club member
*****

Registered: 04/03/09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
Here is one that Shawn came up with for me. Not exactly what you are asking for, but might give you some ideas.

 Code:

$Form1 = $System.Form()
$Form1.StartPosition = 2  ;FormStartPosition_WindowsDefaultLocation
$Form1.Size = $System.Size(640,480) ;(Width,Height)
$Form1.Text = "User Account Manager - Release "+$Version
$Form1.ShowInTaskbar = "True"
$Form1.ControlBox = "False" 
$Form1.MaximizeBox = "False" 
$Form1.MinimizeBox = "True" 


$TextBox1 = $System.TextBox()
$TextBox1.Left = 130
$TextBox1.Text = ""
$TextBox1.Top = 7
$TextBox1.Width = 104
$TextBox1.KeyUp = "TextBox_KeyUp()"
$nul = $Form1.Controls.Add($TextBox1)

$PrimaryAttributes = "Name","SamAccountName"

$UserArray = FnLDAPQuery($PrimaryAttributes,,"(objectClass=User)",,"SUBTREE" )

Function TextBox_KeyUp() 

Dim $Len,$Pos,$KeyCode,$i
 $Len = Len($TextBox1.Text) 
 $Pos = $TextBox1.SelectionStart 
 
 $KeyCode = $System.EventArgs.KeyCode 

 If $KeyCode = 13 
 $textbox1.text = $userarray[$flag2,1]
  Return
 EndIf 

 For $i = 0 to UBound($UserArray) 
  If $Len > 0 
   If Left($UserArray[$i,0],$Len) = $TextBox1.Text 
    $TextBox1.Text = $UserArray[$i,0] 
    $TextBox1.SelectionStart = $Pos 
    $TextBox1.SelectionLength = 999 
    $Flag2 = $i
   EndIf 
  EndIf 
 Next 

EndFunction 

Function fnLDAPQuery($What,Optional $From,Optional $Filter,Optional $OrderBy,Optional $Scope,
    Optional $User,Optional $Pswd)
    
    Dim $oCon,$oCMD,$oRS,$sQ,$aR,$C,$R
    
    $sQ="<"+IIf($From="","LDAP://"+GetObject("LDAP://rootDSE").Get("defaultNamingContext"),
        $From)+">;"+$Filter+";"+IIf(VarType($What)>8192,Join($What,','),$What)+";"+
        IIf($Scope<>"base" And $Scope<>"onelevel","subtree",$Scope)
    
    $oCon=CreateObject("ADODB.Connection")
    $oCon.Provider="ADsDSOObject"
    $oCon.Properties("Encrypt Password").Value=1
    $oCon.Properties("ADSI Flag").Value=1
    If $User And $Pswd
        $oCon.Properties("User ID").Value=$User
        $oCon.Properties("Password").Value=$Pswd
    EndIf
    $oCon.Open("Active Directory Provider")
     
    $oCMD=CreateObject("ADODB.Command")
    $oCMD.ActiveConnection=$oCon
    $oCMD.CommandText=$sQ
    $oCMD.Properties("Page Size").Value=1000
    $oCMD.Properties("Timeout").Value=30
    $oCMD.Properties("Cache Results").Value=0
    
    If InStr($OrderBy,"distinguishedName")
        $oRS=CreateObject("ADODB.Recordset")
        $oRS.CursorLocation=3
        $oRS.Sort=$OrderBy
        $oRS.Open($sQ,$oCon,0,1,1)
    Else
        If $OrderBy
            $oCMD.Properties("Sort On").Value=$OrderBy
        EndIf
        $oRS=$oCMD.Execute
    EndIf
    If @ERROR Exit @ERROR EndIf
    If $oRS.BOF And $oRS.EOF Exit @ERROR EndIf
    
    $aR = $oRS.GetRows()
    Dim $aFR[UBound($aR,2),UBound($aR,1)]
    For $R=0 to UBound($aR,2)
        For $C=0 to UBound($aR,1)
            $aFR[$R,$C]=$aR[$C,$R]
        Next
    Next
    
    $fnLDAPQuery=$aFR
EndFunction
_________________________
Today is the tomorrow you worried about yesterday.

Top
#193997 - 09/05/22 01:08 PM Re: need help with creating dynamic arrays. [Re: Gargoyle]
Glenn Barnas Moderator Offline
KiX Supporter
*****

Registered: 03/01/28
Posts: 3829
Loc: New Jersey
Richard - is that an example of Poolean logic? \:D

Glenn
_________________________
Visit the Inno-Tech web site for Kix tools/UDFs
Lost:Rocket, appx 8' tall, Green w/ yellow fins, nose, and computer bay.. Last seen streaking skyward in NY

Top
#193998 - 09/05/22 02:01 PM Re: need help with creating dynamic arrays. [Re: Glenn Barnas]
Richard H. Administrator Offline
Administrator
*****

Registered: 00/01/24
Posts: 4940
Loc: Leatherhead, Surrey, UK
 Originally Posted By: Glenn "Don't Give Up The Day Job" Barnas
Richard - is that an example of Poolean logic?


<groan> Kindly leave the stage...

You get a gold star for spotting it first.

Top
#193999 - 09/05/22 03:56 PM Re: need help with creating dynamic arrays. [Re: Richard H.]
Glenn Barnas Moderator Offline
KiX Supporter
*****

Registered: 03/01/28
Posts: 3829
Loc: New Jersey
Sorry - I was sick on "bad joke day" (Thursdays, around here) so took a cheap shot on early Friday. ;\)

Great example, by the way!

Glenn
_________________________
Visit the Inno-Tech web site for Kix tools/UDFs
Lost:Rocket, appx 8' tall, Green w/ yellow fins, nose, and computer bay.. Last seen streaking skyward in NY

Top
#194000 - 09/05/23 12:27 AM Re: need help with creating dynamic arrays. [Re: Glenn Barnas]
NTDOC Administrator Offline
Administrator
*****

Registered: 00/07/28
Posts: 11398
Loc: CA
 Originally Posted By: Glenn Barnas
Great example, by the way!


Yes indeed.

Top
Page 1 of 1 1


Moderator:  Bryce, Jochen, Howard Bullock, Allen, Sealeopard, Radimus, Kdyer, Chris S., Glenn Barnas, Benny69, ShaneEP, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 42 anonymous users online.
Newest Members
OKB478, degla, qrstuv833549, bobbyjoe, MikeMcAnulty
16759 Registered Users

Generated in 0.038 seconds in which 0.013 seconds were spent on a total of 14 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org