|
-
Aug 23rd, 2009, 03:50 PM
#1
Thread Starter
Member
Replace Array with Array
Hi, I'm trying to make a project for a type of encryption - but one of the steps is screwing up.
This is my code: (The step that is screwing up)
Code:
Public Sub l2n(ByVal origtext As String)
Dim old As Array
Dim newtxt As Array
old = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "!", "?", """", ",", ":", ";", ".", "`", """, ""+", "-", "*", "@", "#", "$", "%", "^", "&", "(", ")", "{", "}", "[", "]", "/"}
newtxt = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52"}
Replace(origtext, old, newtxt)
End Sub
However apparently an array cannot be converted into a string, and with 52 values (as well as 26 being repeated once) I really do not wish to go through and type a replace for each and every string.
Thanks,
-Arightwizard
(Visual basic 2010 - .net 4.0 framework)
**Edit** Never mind, I think I got it.
***Edit*** OK, it isn't working for me. Can someone respond?
Last edited by Arightwizard; Aug 23rd, 2009 at 04:15 PM.
-
Aug 23rd, 2009, 04:49 PM
#2
Re: Replace Array with Array
Use a For loop to loop through the array. This way, you are replacing each and every string separately, but the for loop spares you from having to type it out 52 times.
-
Aug 23rd, 2009, 04:59 PM
#3
Thread Starter
Member
Re: Replace Array with Array
 Originally Posted by NickThissen
Use a For loop to loop through the array. This way, you are replacing each and every string separately, but the for loop spares you from having to type it out 52 times.
I get what you're saying, but there's one problem:
I have no idea where to start.
All I can tell to do is write
I understand that I need to find a specified string in the array and replace it with the corresponding string in the other array, but I do not know how to label each individual string.
Thank you,
-Arightwizard
-
Aug 23rd, 2009, 05:05 PM
#4
Re: Replace Array with Array
Maybe like this:
vb Code:
Private Function ReplaceSpl(ByVal originalText As String, ByVal oldArr() As String, ByVal newArr() As String) For i As Integer = 0 To oldArr.Length originalText = originalText.Replace(oldArr(i), newArr(i)) Next Return originalText End Function
And you can call it like this:
vb Code:
origtext = ReplaceSpl(origtext, old, newtxt)
-
Aug 23rd, 2009, 05:11 PM
#5
Thread Starter
Member
Re: Replace Array with Array
 Originally Posted by Pradeep1210
Maybe like this:
vb Code:
Private Function ReplaceSpl(ByVal originalText As String, ByVal oldArr() As String, ByVal newArr() As String)
For i As Integer = 0 To oldArr.Length
originalText = originalText.Replace(oldArr(i), newArr(i))
Next
Return originalText
End Function
And you can call it like this:
vb Code:
origtext = ReplaceSpl(origtext, old, newtxt)
Thank you for your response, however now I am getting an error, "Index was outside the bounds of the Array."
This is confusing me.
Thanks,
-Arightwizard
-
Aug 23rd, 2009, 05:15 PM
#6
Re: Replace Array with Array
The loop runs once too much. The Length property of an array returns the number of items, but since that array is zero based (the first item has index 0), there is no item with index "Length".
For example, suppose your array has 3 items. Their indeces would be 0, 1 and 2. But index 3 would not exist.
So, you just need to loop one time less. I'm sure you can figure that out without a code sample
-
Aug 23rd, 2009, 05:24 PM
#7
Thread Starter
Member
Please help
 Originally Posted by NickThissen
The loop runs once too much. The Length property of an array returns the number of items, but since that array is zero based (the first item has index 0), there is no item with index "Length".
For example, suppose your array has 3 items. Their indeces would be 0, 1 and 2. But index 3 would not exist.
So, you just need to loop one time less. I'm sure you can figure that out without a code sample 
I did as Pradeep said, and (like you implied) the same error was returned. Then I changed it to subtract, and still the error was returned. I then changed it to subtract twice, and no error returns but also it does not do what I want it to do...So the topic of this has changed, if anybody can help me.
Just in case, I'll post my full code here:
orig = textbox containing text that is wished to be encrypted.
newtxt = textbox that contains the output after orig.Text is encrypted. Also variables in class 'enc,' but that shouldn't matter.
Code:
Public Class main
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim enc As New enc
enc.ln2l(orig.Text)
End Sub
End Class
Public Class enc
Dim brandnew1 As String
Dim brandnew2 As String
Public Sub ln2l(ByVal origtext As String)
Dim lettersnsymbols As Array
Dim one As String
Dim two As String
Dim three As String
Dim four As String
Dim five As String
Dim six_one As String
Dim six_two As String
Dim seven As String
Dim eight As String
Dim main As New main
Dim newletter As Array
lettersnsymbols = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "!", "?", """", ",", ":", ";", ".", "`", """, ""+", "-", "*", "@", "#", "$", "%", "^", "&", "(", ")", "{", "}", "[", "]", "/"}
newletter = {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""}
n2l(origtext)
one = brandnew2
two = StrReverse(one)
l2n(two)
three = brandnew1
four = Replace("[^A-Za-z0-9]", "", three)
five = ReplaceSpl(four, lettersnsymbols, newletter)
six_one = Len(five)
six_two = five * six_one
seven = six_two / 2
n2l(seven)
eight = brandnew2
main.newtxt.Text = eight
End Sub
Private Function ReplaceSpl(ByVal originalText As String, ByVal oldArr() As String, ByVal newArr() As String)
Dim realength As Integer = oldArr.Length - 2
For i As Integer = 0 To realength
originalText = originalText.Replace(oldArr(i), newArr(i))
Next
Return (originalText)
End Function
Public Sub l2n(ByVal origtext As String)
Dim old As Array
Dim newtxt As Array
old = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "!", "?", """", ",", ":", ";", ".", "`", """, ""+", "-", "*", "@", "#", "$", "%", "^", "&", "(", ")", "{", "}", "[", "]", "/"}
newtxt = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52"}
brandnew1 = ReplaceSpl(origtext, old, newtxt)
End Sub
Public Sub n2l(ByVal origtext As String)
Dim old As Array
Dim newtxt As Array
old = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "!", "?", """", ",", ":", ";", ".", "`", """, ""+", "-", "*", "@", "#", "$", "%", "^", "&", "(", ")", "{", "}", "[", "]", "/"}
newtxt = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52"}
brandnew2 = ReplaceSpl(origtext, newtxt, old)
End Sub
End Class
Last edited by Arightwizard; Aug 23rd, 2009 at 05:35 PM.
-
Aug 23rd, 2009, 05:25 PM
#8
Re: Replace Array with Array
 Originally Posted by NickThissen
The loop runs once too much. The Length property of an array returns the number of items, but since that array is zero based (the first item has index 0), there is no item with index "Length".
For example, suppose your array has 3 items. Their indeces would be 0, 1 and 2. But index 3 would not exist.
So, you just need to loop one time less. I'm sure you can figure that out without a code sample 
Aah.. my bad. I didn't test it before posting.
It should have been Length - 1
vb.net Code:
Private Function ReplaceSpl(ByVal originalText As String, ByVal oldArr() As String, ByVal newArr() As String)
For i As Integer = 0 To oldArr.Length - 1 '<-- should be Length -1 here
originalText = originalText.Replace(oldArr(i), newArr(i))
Next
Return originalText
End Function
-
Aug 23rd, 2009, 05:33 PM
#9
Re: Please help
 Originally Posted by Arightwizard
I did as you said, and the same error was returned. Then I changed it to subtract twice, and no error returns but also it does not do what I want it to do...So the topic of this has changed, if anybody can help me.
Just in case, I'll post my full code here:
orig = textbox containing text that is wished to be encrypted.
newtxt = textbox that contains the output after orig.Text is encrypted. Also variables in class 'enc,' but that shouldn't matter.
It won't work for what you have posted above.
See this part:
Code:
lettersnsymbols = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "!", "?", """", ",", ":", ";", ".", "`", """, ""+", "-", "*", "@", "#", "$", "%", "^", "&", "(", ")", "{", "}", "[", "]", "/"}
newletter = {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""}
...
five = ReplaceSpl(four, lettersnsymbols, newletter)
You are trying to replace those characters with empty strings. So the output string would eventually be an empty string.
Also the lengths of two arrays passed to that ReplaceSpl function should be same.
-
Aug 23rd, 2009, 05:37 PM
#10
Thread Starter
Member
Re: Please help
 Originally Posted by Pradeep1210
It won't work for what you have posted above.
See this part:
Code:
lettersnsymbols = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "!", "?", """", ",", ":", ";", ".", "`", """, ""+", "-", "*", "@", "#", "$", "%", "^", "&", "(", ")", "{", "}", "[", "]", "/"}
newletter = {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""}
...
five = ReplaceSpl(four, lettersnsymbols, newletter)
You are trying to replace those characters with empty strings. So the output string would eventually be an empty string.
Also the lengths of two arrays passed to that ReplaceSpl function should be same.
Actually, believe it or not, that seems strange to me. BECAUSE L2N converts all of the letters to numbers as well as some symbols. I had that step after it has done that, removing any spare symbols (I am sure no letters would be left over) so it would still leave the numbers. Thanks for replying, though.
Also, I made sure the lengths were the same by copying that array into notepad and backspacing out of all the strings to make each one of the 77 strings a null, then I pasted back into VB.
(I just realized this step is redundant - if you check the L2N area it converts every one of those symbols to numbers, so there would be none left over. But oh well, there's no need to remove or keep it in there so I'll keep it in there.)
Last edited by Arightwizard; Aug 23rd, 2009 at 05:47 PM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|