Results 1 to 10 of 10

Thread: Replace Array with Array

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    47

    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.

  2. #2

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    47

    Re: Replace Array with Array

    Quote Originally Posted by NickThissen View Post
    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
    Code:
    For each ?
    Next
    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

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Replace Array with Array

    Maybe like this:
    vb Code:
    1. Private Function ReplaceSpl(ByVal originalText As String, ByVal oldArr() As String, ByVal newArr() As String)
    2.     For i As Integer = 0 To oldArr.Length
    3.         originalText = originalText.Replace(oldArr(i), newArr(i))
    4.     Next
    5.     Return originalText
    6. End Function

    And you can call it like this:
    vb Code:
    1. origtext = ReplaceSpl(origtext, old, newtxt)
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    47

    Re: Replace Array with Array

    Quote Originally Posted by Pradeep1210 View Post
    Maybe like this:
    vb Code:
    1. Private Function ReplaceSpl(ByVal originalText As String, ByVal oldArr() As String, ByVal newArr() As String)
    2.     For i As Integer = 0 To oldArr.Length
    3.         originalText = originalText.Replace(oldArr(i), newArr(i))
    4.     Next
    5.     Return originalText
    6. End Function

    And you can call it like this:
    vb Code:
    1. 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

  6. #6
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    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

  7. #7

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    47

    Please help

    Quote Originally Posted by NickThissen View Post
    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.

  8. #8
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Replace Array with Array

    Quote Originally Posted by NickThissen View Post
    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:
    1. Private Function ReplaceSpl(ByVal originalText As String, ByVal oldArr() As String, ByVal newArr() As String)
    2.     For i As Integer = 0 To oldArr.Length - 1       '<-- should be Length -1 here
    3.         originalText = originalText.Replace(oldArr(i), newArr(i))
    4.     Next
    5.     Return originalText
    6. End Function
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  9. #9
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Please help

    Quote Originally Posted by Arightwizard View Post
    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.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  10. #10

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    47

    Re: Please help

    Quote Originally Posted by Pradeep1210 View Post
    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", "!", "?", """", ",", ":", ";", ".", "`", """, ""+", "-", "*", "@", "#", "$", "&#37;", "^", "&", "(", ")", "{", "}", "[", "]", "/"}
            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
  •  



Click Here to Expand Forum to Full Width