Results 1 to 9 of 9

Thread: How do I set this up ? (good array example)[RESOLVED]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    TZI Transition Date
    Posts
    272

    Resolved How do I set this up ? (good array example)[RESOLVED]

    How would I cycle through these 4 (actually a bunch more) pairs until they are all processed by the same code ...it's probably a For Each Array solution but I've never had to do it before... tkx

    str1="stuff"
    str2="otherstuff"

    str1="stuff"
    str2="otherstuff"

    str1="stuff"
    str2="otherstuff"

    str1="stuff"
    str2="otherstuff"

    'some code to do something with str1 and str2

    'get the next pair and do it again until all pairs processed
    Last edited by MJBNET; Feb 11th, 2005 at 11:42 AM. Reason: forgot to checkmark it...

  2. #2
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457

    Re: How do I set this up ?

    what is your data source. A text file or are the strings in text box's on a form ?

    .
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    TZI Transition Date
    Posts
    272

    Re: How do I set this up ?

    It's a text file...

  4. #4
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457

    Re: How do I set this up ?

    Comma or tab delimited ??
    A pair of strings per line or one string per line? The former would be better as it makes differentiation of the data more positive.
    .
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    TZI Transition Date
    Posts
    272

    Re: How do I set this up ?

    like this in the file:
    text str1
    text str2
    (space)
    text str1
    text str2
    (space)
    text str1
    text str2
    (space)
    etc.

  6. #6
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457

    Re: How do I set this up ?

    No Probs. Just trying some code now...
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  7. #7
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457

    Re: How do I set this up ?

    OK - This code works wth the text file in the format that you called for. But it is quick and dirty with regards how it handles the file. You have to have two carriage returns after the last string (in the text file) and no spaces at the top of the file.

    You will get the idea from this though, and maybe you will feel that you could work it better with a different file format. I would go for comma or tab delimited strings. Two to a line.

    Anyway, here you go...

    VB Code:
    1. Function LoadAndOperate() As Boolean
    2.  
    3. Dim n As Long
    4. Dim s1 As String, s2 As String
    5.  
    6. Begin:
    7.  
    8.      On Error GoTo Module_Error
    9.      LoadAndOperate = False
    10.      
    11.      Dim Buffer As String
    12.      Dim File2WorkOn
    13.      
    14.      'Load the file into a line array
    15.      Dim Linecount, LineArray, filenum, OneLine, Line1, Line2, skipline
    16.      ReDim LineArray(0 To 1, 0 To 10)
    17.  
    18.      File2WorkOn = "c:\test.txt"
    19.      filenum = FreeFile
    20.      
    21.      Open File2WorkOn For Input As #filenum
    22.      '--------------------------
    23.      n = 0
    24.      Do While Not EOF(filenum)
    25.           ' assuming no space at the start of the line..
    26.          
    27.           Line Input #filenum, Line1
    28.           Line Input #filenum, Line2
    29.           Line Input #filenum, skipline
    30.          
    31.           ' Load each string ....
    32.           LineArray(0, n) = Line1
    33.           LineArray(1, n) = Line2
    34.          
    35.           n = n + 1
    36.           ' Make more space in the array if we need to....
    37.           If n > UBound(LineArray) Then
    38.                ReDim Preserve LineArray(0 To 1, 0 To (UBound(LineArray) + 10))
    39.                End If
    40.      Loop
    41.      
    42.      Close #filenum
    43.  
    44.      ReDim Preserve LineArray(0 To 1, 0 To n - 1)     ' EDIT - OOPS Forgot this first time in              
    45.          
    46.      For n = 0 To UBound(LineArray, 2)
    47.          
    48.           s1 = LineArray(0, n)
    49.           s2 = LineArray(1, n)
    50.          
    51.           ' Work with S1 and S2 here....
    52.                    
    53.      Next n
    54.    
    55.          
    56.      LoadAndOperate = True
    57. Module_Exit:
    58.      On Error GoTo 0
    59.      Exit Function
    60.  
    61. Module_Error:
    62.      MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure LoadAndOperate "
    63.      GoTo Module_Exit
    64. End Function
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    TZI Transition Date
    Posts
    272

    Re: How do I set this up ?

    Thanks a bunch David ! not only does it work straitaway, it's simple enough so I can learn how it works and go from there (since I knew nothing about setting up arrays)

  9. #9
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457

    Re: How do I set this up ? (good array example)[RESOLVED]

    You are welcome. The trick with arrays is not to define them fully at the declaration point - unless you want to that is. Because if you do you can't change them later.

    Dim LineArray

    can be redimentioned, but

    Dim Linearray(0 to 1, 0 to 10)
    Dim Linearray(10)

    are fixed.

    Also when you redimention an array, to save what was already in there use the "preserve" keyword. This only works however if you extend the LAST dimention in the array.

    For example. In the code I posted, I could not have added more room for another string (making it able to handle three strings instead of two) without clearing down the array and starting again. I tend to think of it that it is the structure of the array that is fixed, but how many items you have in the array can be extended.

    Have fun...

    You also have UDT's (User defined types), Collections and Dictionary types to play with yet.
    .
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

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