home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.visual.basic      MS Visual Basic discussions, NOT dot-net      10,840 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 9,911 of 10,840   
   Paul M. Cook to Steve Gerrard   
   Re: Null values in variant array   
   25 Feb 06 03:09:28   
   
   From: pmBERMUDA_SHORTScook@gte.net   
      
   "Steve Gerrard"  wrote in message   
   news:UJWdnVVt0dMNVWLeRVn-qQ@comcast.com...   
   >   
   > "Paul M. Cook"  wrote in message   
   > news:w1NLf.92$pE4.64@trnddc04...   
   > > Let's say you have a CSV file and you load it into a variant array using   
   the   
   > > split function on VBCrLF.  Then you load a variable with the line count   
   and   
   > > loop through the array for 0 to line count.  This works well unless you   
   have   
   > > blank lines at the end of the CSV file.  Now if you process the loop,   
   you'll   
   > > get an out of bounds subscript at the end of the loop because you are   
   > > referencing null values at the end of the variant array.   
   > >   
   > > How would you go about stripping the extraneous CRLFs from the end of   
   the   
   > > file?   
   > >   
   >   
   > If it is just an out of bounds subscript, then use UBound(MyArray) instead   
   of   
   > the line count. That will tell you how many actual array entries you have.   
   >   
   >   
      
   Here is some of the code:   
      
      
   Dim whole_file As String   
   Dim lines As Variant   
   Dim the_array() As Variant   
      
      
         Open InDirPath For Input As #1   
      
         ' Load the file into a memory string   
      
         whole_file = Input$(LOF(1), 1)   
      
         Close #1   
      
         ' Break the file into lines.   
         lines = Split(whole_file, vbCrLf)   
      
         ' Dimension the array.   
         num_rows = UBound(lines)   
         ReDim the_array(num_rows)   
      
         ' Copy the data into the array.   
         For R = 0 To num_rows   
           the_array(R) = Split(lines(R), ",")   
         Next R   
      
      
   In my case, num_rows will include a count representing the extra CRs at the   
   end of the file.  So if the file looks like this:   
      
   1234,abcd,9876   
   2334,kjkjk,0008   
      
      
      
   Then num_rows will be 4.   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca