My Assistant
![]() ![]() |
|
|
Dec 15 2004, 06:28 PM
Post
#1
|
|
|
UtterAccess Addict Posts: 213 From: Australia |
Hello Utteracccess,
i was wondering how i would go about joinging to words. Examples is. word1 = Utter word2 = Access joined word = UATCTCEERSS so it takes one letter from each word and makes a total string. i have tried but just can't seem to wrap my head around it at the moment. Thanks Also just another quick Question if i use the code below why does it not work Dim sTemp as string, sName as string sName = "Utteraccess" sTemp = strConv(sName, vbFromUnicode) Debug.print sTemp When i debug.print i get ??? not what i thought i was meant to get Any Help Thanks. |
|
|
|
Dec 15 2004, 07:15 PM
Post
#2
|
|
|
VIP Emeritus Posts: 1,340 |
Out of curiosity why do you want to join the strings in that way?
|
|
|
|
Dec 15 2004, 07:19 PM
Post
#3
|
|
|
UtterAccess Addict Posts: 213 From: Australia |
Basically two reasons.
1. So i can join them and do a few more calculation for a activation key 2. Also some cases for scrambling data of non active records HTH trip |
|
|
|
Dec 16 2004, 12:38 AM
Post
#4
|
|
|
UtterAccess Editor Posts: 15,965 From: Northern Virginia, USA |
Try this ... The pasted code ScrambleWords() will scramble as many words as you want and if the words are of different lengths, will put a character (constrFillerCharacter) for the "missing" character.
Hope it works for you ... the included 'Test' sub (which combined "Word1", "Word2", "MyExtraLongWord") returned .. WWMooyrrEddx12t^^r^^a^^L^^o^^n^^g^^W^^o^^r^^d which seems to be what you are after ... The code pasted below is also attached to this post as a text file. CODE Public Sub Test()
Debug.Print ScrambleWords("Word1", "Word2", "MyExtraLongWord") End Sub Public Function ScrambleWords(ParamArray varWordList() As Variant) As String 'Scrambles words together Const constrFillerCharacter = "^" 'Filler character used if all the words are not the same length Dim aintLength() As Integer 'The length of each word passed to the function Dim intMaxLength As Integer 'The maximum char length in the word list Dim strResult As String 'the scrambled word Dim x As Integer 'a counter Dim y As Integer 'a counter 'Rediminsion the aintLength array ReDim aintLength(UBound(varWordList())) 'First find the length of all the words you passed into the function and get the max length For x = 0 To UBound(varWordList) aintLength(x) = Len(Trim(CStr(varWordList(x)))) If aintLength(x) > intMaxLength Then intMaxLength = aintLength(x) End If Next x 'Next build the word by looping to the max number of characters and adding that count character 'from each each word if the word has that many characters ... For y = 1 To intMaxLength For x = 0 To UBound(varWordList()) If y <= aintLength(x) Then strResult = strResult & Mid(CStr(varWordList(x)), y, 1) Else strResult = strResult & constrFillerCharacter End If Next x Next y 'return the result ScrambleWords = strResult End Function |
|
|
|
Dec 16 2004, 02:39 AM
Post
#5
|
|
|
UtterAccess Addict Posts: 213 From: Australia |
datAdrenaline Thank you for your example it will be very helpfull.
you have saved me alot of time thank you very much. |
|
|
|
Dec 16 2004, 02:40 AM
Post
#6
|
|
|
Retired Moderator Posts: 10,959 From: Prague,CZ / Kiev,UA |
Interesting request (IMG:http://www.utteraccess.com/forum/style_emoticons/default/frown.gif)
Here're my two cents: Sub TryScramble() regards, martin |
|
|
|
Dec 16 2004, 02:28 PM
Post
#7
|
|
|
UtterAccess Editor Posts: 15,965 From: Northern Virginia, USA |
Martin ... I love reading your code! ... I always seem to find some "trick"... like the Byte arrays ... as always, quite efficient!!
And for trip here is yet another way .... This method will loop back to beginning of a word if the length of the current word is shorter than the character being sought .. the code is a little harder to read/understand....but here it is in the event you would rather use it... CODE Function ScrambleWords(ParamArray varWordList()) As String
Dim intMax As Integer Dim i As Integer Dim x As Integer Dim intCharacterPos As Integer Dim intPass As Integer Dim intLen As Integer Dim strResult As String 'Initialize i = 1 intMax = 0 'Loop all the words in the word list On Error Resume Next Do Until i >= intMax And i <> 1 For x = 0 To UBound(varWordList()) intLen = Len(Trim(CStr(varWordList(x)))) If intLen > intMax Then intMax = intLen End If intPass = Int(i / intLen) + IIf(i Mod intLen, 1, 0) intCharacterPos = i - (intPass - 1) * intLen strResult = strResult & Mid(varWordList(x), intCharacterPos, 1) Next x i = i + 1 Loop ScrambleWords = strResult End Function |
|
|
|
Dec 16 2004, 06:52 PM
Post
#8
|
|
|
Retired Moderator Posts: 10,959 From: Prague,CZ / Kiev,UA |
Hello Brent,
I thought you might like this approach. Following your idea with Param Array, let's play further (IMG:http://www.utteraccess.com/forum/style_emoticons/default/frown.gif) : Sub TryScramble2() I am not saying this code is faster then yours as it needs two nested loops (but it may be). It was just fun writing it (IMG:http://www.utteraccess.com/forum/style_emoticons/default/frown.gif) . Martin |
|
|
|
Dec 17 2004, 01:00 AM
Post
#9
|
|
|
UtterAccess Editor Posts: 15,965 From: Northern Virginia, USA |
Cool ... (IMG:http://www.utteraccess.com/forum/style_emoticons/default/compute.gif) ... That is definately not something to show a newbie! ... I love arrays!
To add to the fun I created a DB to test and time our procedures ... Quick result ... the first one I posted was fastest with two words (but not by much!!) and the code posted in this post was the fastest for 4 words .... The caveat is that I took out the Trim() & CStr() functions. I ended up taking the Trim() and CStr() out of all my procedures, and I UCase()'d the result in order to compare the result easily. ALL the procedures took less than a millesecond to run, however, on occasion they all went slightly above 1 millisecond, so for my test, I ran each one 10000 times and then took the average. My second one was slowest due to the DIVISION and MOD operations (which on the binary level take several clock cycles) used to loop back to the beginning of the "short" words.... So I created yet another version ... CODE Public Function ScrambleWords_B3(ParamArray varWordList()) As String
Dim intMax As Integer Dim i As Integer Dim x As Integer Dim intLen As Integer Dim strResult As String 'Initialize i = 1 intMax = 0 'Loop all the words in the word list On Error Resume Next Do Until i > intMax And i <> 1 For x = 0 To UBound(varWordList()) intLen = Len(varWordList(x)) If intLen > intMax Then intMax = intLen End If If i <= intLen Then strResult = strResult & Mid(varWordList(x), i, 1) Next x i = i + 1 Loop ScrambleWords_B3 = UCase(strResult) End Function Which is the same as my second ... except for the looping back on the short words ... It will produce the same result as Kings 2nd code set. Here are the results ... (Remember each proc was ran 1000 times .. the speed is the average) CODE Word List: aaaaaaaaaa, bbbbb
---------------------------------------------------- Procedure Result Milliseconds ---------------------------------------------------- Brent1 ABABABABABA^A^A^A^A^ 0.0514 Brent2 ABABABABABABABABABAB 0.0976 Brent3 ABABABABABAAAAA 0.0575 King1 ABABABABABAAAAA 0.0551 King2 ABABABABABAAAAA 0.0829 Word List: aaaaaaaaaa, bbbbb, ccccc, ddddd ------------------------------------------------------------------ Procedure Result Milliseconds ------------------------------------------------------------------ Brent1 ABCDABCDABCDABCDABCDA^^^A^^^A^^^A^^^A^^^ 0.0811 Brent2 ABCDABCDABCDABCDABCDABCDABCDABCDABCDABCD 0.1663 Brent3 ABCDABCDABCDABCDABCDAAAAA 0.0856 King2 ABCDABCDABCDABCDABCDAAAAA 0.14 Note: My setup is Win98, A2000, PIII - 1100mhz To get the time, I used the function GetTickCount from kernal32.dll before and after the function call. I have attached the A2000 database with the table of data, a query for the results and all the code samples for those of you who like to "Peg the Geek Meter" (IMG:http://www.utteraccess.com/forum/style_emoticons/default/thumbup.gif) (IMG:http://www.utteraccess.com/forum/style_emoticons/default/laugh.gif) (IMG:http://www.utteraccess.com/forum/style_emoticons/default/tongue.gif) (IMG:http://www.utteraccess.com/forum/style_emoticons/default/ohyeah.gif) |
|
|
|
Dec 17 2004, 06:49 PM
Post
#10
|
|
|
Retired Moderator Posts: 10,959 From: Prague,CZ / Kiev,UA |
Hello again Brent,
I ran the tests but somehow [censored] find the results. The bottleneck of our approaches so far is the concatenation which is a very expensive operation in VBA. Working with Byte Array can often eliminate the need of string concatenation - as in the following example: (Try to test it, should be faster then my last code) Function ScrambleWords3(ParamArray Words() As Variant) As String (Terrence, sorry for kinda hijacking your post (IMG:http://www.utteraccess.com/forum/style_emoticons/default/frown.gif) ) Martin |
|
|
|
Dec 18 2004, 01:34 AM
Post
#11
|
|
|
UtterAccess Addict Posts: 213 From: Australia |
Wow looks like everyone is enjoying this.. and i myself and very thankfull you have not only provided the answer to my question but have also kept on improving.
Right on people (IMG:http://www.utteraccess.com/forum/style_emoticons/default/thumbup.gif) (IMG:http://www.utteraccess.com/forum/style_emoticons/default/yayhandclap.gif) (IMG:http://www.utteraccess.com/forum/style_emoticons/default/uarulez2.gif) |
|
|
|
Dec 18 2004, 11:58 PM
Post
#12
|
|
|
UtterAccess Editor Posts: 15,965 From: Northern Virginia, USA |
Well Mr. King ... Your last posted code was faster than your previous code, however, the result of the function did not produce the correct result ... here are the speed results .. Brent1, Brent3, and King1 are still the quickest of all the previous posts for the 2 word test, with Brent1 beating King1 and Brent3 by 0.0004 milliseconds. Brent1 and King 1 tied. Also with respect to the previous posts, Brent1 and Brent3 are still the fastest for the 4 word test, with Brent1 overcoming the previous champ Brent3 .... However, there is a new kid on the block, Brent4, which is a modifed version of Brent1. Brent4 has the "filler chararacter" concatenation removed. This little tweak allowed the Rookie to sweep the pole position in the latest round of testing ... Here are the results (Brent4 code posted at the end) ... Also the lastest database with the tests and code is attached (A2000 format)
CODE Word List: aaaaaaaaaa, bbbbb --- 10000 Tests -------------------------------------------------------------------- Procedure Result Milliseconds -------------------------------------------------------------------- Brent4 ABABABABABAAAAA 0.0511 Brent1 ABABABABABA^A^A^A^A^ 0.0536 King1 ABABABABABAAAAA 0.054 Brent3 ABABABABABAAAAA 0.054 King3 ABBBBBAAAA 0.0617 King2 ABABABABABAAAAA 0.0808 Brent2 ABABABABABABABABABAB 0.0922 Word List: aaaaaaaaaa, bbbbb, ccccc, ddddd --- 10000 Tests -------------------------------------------------------------------- Procedure Result Milliseconds -------------------------------------------------------------------- Brent4 ABCDABCDABCDABCDABCDAAAAA 0.0728 Brent1 ABCDABCDABCDABCDABCDA^^^A^^^A^^^A^^^A^^^ 0.0768 Brent3 ABCDABCDABCDABCDABCDAAAAA 0.0811 King3 ABCDBCDBCDBCDBCDAAAA 0.0952 King2 ABCDABCDABCDABCDABCDAAAAA 0.1327 Brent2 ABCDABCDABCDABCDABCDABCDABCDABCDABCDABCD 0.1588 Here is the Brent4 code CODE Public Function ScrambleWords_B4(ParamArray varWordList() As Variant) As String 'Scrambles words together Dim aintLength() As Integer 'The length of each word passed to the function Dim intMaxLength As Integer 'The maximum char length in the word list Dim strResult As String 'the scrambled word Dim x As Integer 'a counter Dim y As Integer 'a counter 'Rediminsion the aintLength array ReDim aintLength(UBound(varWordList())) 'First find the length of all the words you passed into the function and get the max length For x = 0 To UBound(varWordList) aintLength(x) = Len(varWordList(x)) If aintLength(x) > intMaxLength Then intMaxLength = aintLength(x) End If Next x 'Next build the word by looping to the max number of characters and adding that count character 'from each each word if the word has that many characters ... For y = 1 To intMaxLength For x = 0 To UBound(varWordList()) If y <= aintLength(x) Then strResult = strResult & Mid(varWordList(x), y, 1) End If Next x Next y 'return the result ScrambleWords_B4 = UCase(strResult) End Function Even though Brent2 was the slowest, I like it the best! (IMG:http://www.utteraccess.com/forum/style_emoticons/default/frown.gif) ... But for shear speed, Brent4 takes it ... |
|
|
|
Dec 20 2004, 06:46 PM
Post
#13
|
|
|
Retired Moderator Posts: 10,959 From: Prague,CZ / Kiev,UA |
What???
Byte arrays beaten? (IMG:http://www.utteraccess.com/forum/style_emoticons/default/frown.gif) Anyway, nice discussion, thanks. Regards, Martin |
|
|
|
![]() ![]() |
|
Go to Top · Lo-Fi Version | Time is now: 18th May 2013 - 05:02 PM |