Welcome Guest
Log In | Register )
You last visited December 9, 2016, 11:56 pm
All times shown are
Eastern Time (GMT-5:00)

Need help with Visual Basic 6

Topic closed. 3 replies. Last post 10 years ago by hypersoniq.

Page 1 of 1
PrintE-mailLink
hypersoniq's avatar - 8ball
Pennsylvania
United States
Member #1340
April 6, 2003
2450 Posts
Offline
Posted: May 12, 2007, 2:14 pm - IP Logged

I am almost done automating a system I am working on, I need help loading a CSV file into a 2 dimensional array... here is the code I have so far...

Dim intDrawHistory(1 To 10382, 1 To 3) As Integer

Dim intLoadArray As Integer
'Open statement Here
For intLoadArray = 1 To 10382 Step 1
    'load 1stnumber here =intDrawHistory (intLoadArray,1)
    'load second number here =intDrawHistory (intLoadArray,2)
    'load third number here =intDrawHistory (intLoadArray,3)
Next intLoadArray

The red area is where I'm stuck... I still have to finish the blue placeholder yet to open the file P3.csv which just contains numbers

9,4,3

2,3,4

etc...

I'm trying to avoid the overhead and complexity of using ADO or OLE.

Any help greatly appreciated :-)

Playing more than one ticket per game is betting against yourself.

    Avatar

    United States
    Member #41846
    June 23, 2006
    460 Posts
    Offline
    Posted: May 12, 2007, 8:31 pm - IP Logged

    I am almost done automating a system I am working on, I need help loading a CSV file into a 2 dimensional array... here is the code I have so far...

    Dim intDrawHistory(1 To 10382, 1 To 3) As Integer

    Dim intLoadArray As Integer
    'Open statement Here
    For intLoadArray = 1 To 10382 Step 1
        'load 1stnumber here =intDrawHistory (intLoadArray,1)
        'load second number here =intDrawHistory (intLoadArray,2)
        'load third number here =intDrawHistory (intLoadArray,3)
    Next intLoadArray

    The red area is where I'm stuck... I still have to finish the blue placeholder yet to open the file P3.csv which just contains numbers

    9,4,3

    2,3,4

    etc...

    I'm trying to avoid the overhead and complexity of using ADO or OLE.

    Any help greatly appreciated :-)

    Hypersonic

    First, I don't program in Vbasic. I'm stuck in the past with GWBasic.

    I use basic to load data all the time, in a manner similiar to what you are doing.  So a Question ? You outer loop loads the draw #, Why do you not use an inner loop to load the draw data? see below

     

    for y=1 to 3 step 1 : rem i like to keep my variables simple

     get data  :rem  with gwbasic it would be input #x,z with x being the file number(previously defined  with an open statement) and z would be the data element 

    intdrawhistory(intloadarray,y)=data ( in this case z)

    next y :  rem completes inner loop loads 3 bits of data to the array

     

    if you file contains other info such as date you will need to input that to a dummy variable to keep you data in correct order.

     

    hope this helps

     

    p8

      paurths's avatar - underground
      Switching between Fairfax, VA and Belgium
      Belgium
      Member #19287
      July 29, 2005
      2254 Posts
      Offline
      Posted: May 13, 2007, 6:51 am - IP Logged

      I am almost done automating a system I am working on, I need help loading a CSV file into a 2 dimensional array... here is the code I have so far...

      Dim intDrawHistory(1 To 10382, 1 To 3) As Integer

      Dim intLoadArray As Integer
      'Open statement Here
      For intLoadArray = 1 To 10382 Step 1
          'load 1stnumber here =intDrawHistory (intLoadArray,1)
          'load second number here =intDrawHistory (intLoadArray,2)
          'load third number here =intDrawHistory (intLoadArray,3)
      Next intLoadArray

      The red area is where I'm stuck... I still have to finish the blue placeholder yet to open the file P3.csv which just contains numbers

      9,4,3

      2,3,4

      etc...

      I'm trying to avoid the overhead and complexity of using ADO or OLE.

      Any help greatly appreciated :-)

      Hi hypersoniq,

      first, you should create a multidimensional array as should be done,
      meaning that the second dimension is the one that is flexible (i don't know the correct word in English, sorry...)

      dim intDrawHistory () as Integer

      Then you can redim it, using preserve, to the correct size

      Redim intDrawHistory(3,AnyVariableHere)  '--> 3 represents each digit of the number

      Here is an example, where a textfile is saved as "example.csv" at "c:\example.csv"
      This is what is in the file, for example:
      123
      234
      556
      332

       

      Dim iFile As Integer
      Dim intDrawHistory() As Integer
      Dim iDimension2 As Integer
      Dim sLine As String  'We will collect line per line

          iFile = FreeFile
          iDimension2 = 0 'You use base = 1, so we'll set it to 0 for starters, and later on it will be set to 1

          Open "c:\example.csv" For Input As iFile

          While Not EOF(iFile)
            Line Input #iFile, sLine
            iDimension2 = iDimension2 + 1
            ReDim Preserve intDrawHistory(3, iDimension2)
            intDrawHistory(1, iDimension2) = CInt(Mid(sLine, 1, 1))
            intDrawHistory(2, iDimension2) = CInt(Mid(sLine, 2, 1))
            intDrawHistory(3, iDimension2) = CInt(Mid(sLine, 3, 1))
          Wend

      lasas3

      An onion a day keeps everyone away!!!

        hypersoniq's avatar - 8ball
        Pennsylvania
        United States
        Member #1340
        April 6, 2003
        2450 Posts
        Offline
        Posted: May 13, 2007, 12:13 pm - IP Logged

        Thanks for the input!

        What I ended up doing was hard-coding the number of draws and the resulting array loading code is...

        Dim intFileNumber As Integer
        Dim B1 As Integer
        Dim B2 As Integer
        Dim B3 As Integer
        intReadFileNumber = FreeFile
        Open "C:\P3.csv" For Input As intReadFileNumber
        Dim intLoadArray As Integer
        For intLoadArray = 1 To 10382 Step 1
            Input #intReadFileNumber, B1
            Input #intReadFileNumber, B2
            Input #intReadFileNumber, B3
            intDrawHistory(intLoadArray, 1) = B1
            intDrawHistory(intLoadArray, 2) = B2
            intDrawHistory(intLoadArray, 3) = B3
        Next intLoadArray

        I know I could have used shorter variable names, but I tend to forget what I did and it makes it easier on me to go back and have those descriptive names. (I trimmed out the comments also)

        I set "Option Base 1" in the declarations.

        Playing more than one ticket per game is betting against yourself.