Prev: How to ignore Datatype of ADODB Fields while populating data into
Next: add maskedbox to form on runtime
From: IJALAB on 21 Apr 2010 04:03 Hi, I have a text file which is like 205 -204 203 -102 i want to read each line, split each line elements to arr(0) as 205 and arr(1) as -204 i know the length of the file which is 2048 lines, how do i access arr(0) and arr(1) meaning odd array indices and even array indices sperately post split function. Str1 = Split(tempstr," ") tempstr is the fileinput string I am new to VB i had declared this Str1 as Dim Str1(2048) as String while trying to create array string to read the file input, i wrote Str1 = Split(tempstr," "), i am getting error cannot initialize to array so i gave Str(Count) = Split(tempstr," "), for which i am getting subscript out of range error. i am confused by the behavior.
From: Helmut Meukel on 21 Apr 2010 05:27 "IJALAB" <balaji.draj(a)gmail.com> schrieb im Newsbeitrag news:d33cadbf-a920-43a2-b9fe-f2e06f77bfbe(a)o15g2000pra.googlegroups.com... > Hi, > > I have a text file which is like > 205 -204 > 203 -102 > i want to read each line, split each line elements to arr(0) as 205 > and arr(1) as -204 > i know the length of the file which is 2048 lines, how do i access > arr(0) and arr(1) meaning odd array indices and even array indices > sperately post split function. > Str1 = Split(tempstr," ") > tempstr is the fileinput string > I am new to VB > i had declared this Str1 as Dim Str1(2048) as String > while trying to create array string to read the file input, i wrote > Str1 = Split(tempstr," "), i am getting error cannot initialize to > array > so i gave Str(Count) = Split(tempstr," "), for which i am getting > subscript out of range error. > > i am confused by the behavior. > Didn't you read the online help topic about Split? I clearly says it *returns* an array of string elements. The number of elements depends on the provided string-to-split. I assume your tmpstr contains the whole file? Then the lines will be usually separated by CRLF (if the file was created by a windows program) or by a single CR or a single LF. Split can't work with multible separators, you have first to replace the line separators with a single space. (How to do this was discussed in this very newsgroup just some weeks ago). Then use an uninitialized dynamic array (Dim Str1() as String) for the Split function. After performing the split, Ubound(Str1) should return 2047. You can access the Values in a loop: For i = 0 to Ubound(Str1)-1 Step 2 x = Str1(i) y = Str1(i + 1) ... ' store the data somewhere or set the point in your chart) Next i Alternatively change your file reading so you get each line into an element of an string array and perform the Split for each line separately (looping through the string array) asigning the return values to elements of a variant array. (read about Variants in Online Help). I put together a tiny test app to demonstrate how this works. Create a form, set its AutoRedraw property to true. Put this code into Form_Load: Private Sub Form_Load() Dim a(3) As Variant Dim i as Integer, j as Integer For j = 0 To 3 a(j) = Split("this is Test " & j, " ") ' in your own app you should use Split(tmpstr(j), "") Next j For j = 0 To 3 For i = 0 To UBound(a(j)) Print a(j)(i) Next i Next j End Sub You see the differences in accessing the elements of String arrays stored in a Variant Array. Helmut.
From: Mike Williams on 21 Apr 2010 06:25 "IJALAB" <balaji.draj(a)gmail.com> wrote in message news:d33cadbf-a920-43a2-b9fe-f2e06f77bfbe(a)o15g2000pra.googlegroups.com... > I have a text file which is like > 205 -204 > 203 -102 > i want to read each line, split each line elements to > arr(0) as 205 and arr(1) as -204 . . . I am getting > various errors . . . You have said you are new to VB but you haven't said which version of it you are using. For the time being I will assume it is the real Visual Basic (of which VB6 is the latest version), or perhaps even VBA. If it is something else then you are in the wrong newsgroup. There are all sorts of different ways of reading your data from the file and transferring it into variables that are suitable for your specific needs, but since you have said that you do not understand the behaviour of the Split function then it might be best to describe roughly how it works. You haven't made it clear whether you are reading your data into one long string containing all of it, or in to 2048 invididual strings each containing just one pair of items, so initially I'll deal with it as though you have just one pair of items in a string and you want to Split them into an array of two strings, just so you can see how it works: Firstly, and most importantly, when declaring a string array which you intend to "Split" something into then you need to declare it as a dynamic array (an array that does not initially contain any elements) such as: Dim Str1() As String Then, when you use the Split function to split and dump a string into it, VB will automatically assign as many elements as are required. For example: Dim Str1() As String, tempstr As String tempstr = "205 - 204" Str1 = Split(tempstr, " ") When you run the above code you will end up with Str1 array containing as many elements as VB decided were required, with the first element being element number zero. Try the above code and it should run okay. Then, add to it some more code so that you can see what you ended up with. The full code required would be as follows: Private Sub Command1_Click() Dim Str1() As String, tempstr As String, n As Long tempstr = "205 - 204" Str1 = Split(tempstr, " ") For n = LBound(Str1) To UBound(Str1) Print n & vbTab & Str1(n) Next n End Sub The output will be printed on your Form, showing how many elements ended up in the Str1 array together with their contents. The very first thing you will notice (in this specific example) is the output is not quite what you might have been expecting. This is because your code has told the Split function to "split" wherever it finds a space and that is exactly what it has done. The problem is that (in the specific example you posted and in this example here) the second number contains a space between the minus sign and the number itself. Now try the same code, but this time on a similar small "string containing two values" that you have loaded from your real data file using LineInput. Is there a space between the negative sign and the value in the actual file? If there is then you will need more code to deal with it. As I have said, there are lots of different ways to deal with the data you are apparently working with. You can read the entire file data as binary into a Byte Array or into one long string or into lots of small strings (although the later is not the method I would advise) and you can deal with the loaded data in all sorts of different ways. If you decide to load the entire file into one string and then use a little bit of code to prepare it (if necessary, depending on the data) followed by Split to "split" it then you can effectively "Split" the loaded string into a one dimensional string array containing 2048 elements, myArray(0 to 2047) for example, or with a little bit of trickery you can "Split" it into a two dimensional string array, myArray(0 to 1, 0 to 1023) for example. There are all sorts of possibilities . . . .. . . However, the very first thing you need to do is discover exactly what form the data has (whether there is a space between the minus sign and the value and whether it uses a vbCrLf at the end of each little "205 - 204" line or perhaps just a vbLF or some other line marker). Until you know that information then you will not be able to decide on the best way to handle the data. Here is how you can check the file to see what it actually contains. The following code will load the first 5000 bytes of the file and display them in the ListBox so that you can examine them. Paste the code into a VB Form containing a Command Button and a ListBox and change the hard coded file name to the path and name of your own data file. Have a careful look at the output in the ListBox. Are there actually any space characters between the minus signs and the values, what are the "end of line" character or characters, etc, etc. When you have that information you will be in a much better position to write code to deal with it. Mike Private Sub Command2_Click() Dim b() As Byte, n As Long, s1 As String Open "c:\temp\datafile.txt" For Binary As 1 If LOF(1) < 5000 Then ReDim b(0 To LOF(1) - 1) Else ReDim b(0 To 4999) End If Get 1, 1, b() Close 1 For n = LBound(b) To UBound(b) s1 = Format(n) & vbTab & CStr(b(n)) If b(n) > 31 Then s1 = s1 & vbTab & Chr$(b(n)) End If List1.AddItem s1 Next n End Sub
From: Mike Williams on 21 Apr 2010 08:21 "IJALAB" <balaji.draj(a)gmail.com> wrote in message news:d33cadbf-a920-43a2-b9fe-f2e06f77bfbe(a)o15g2000pra.googlegroups.com... > i want to read each line, split each line elements > to arr(0) as 205 and arr(1) as -204 In my previous response I said, " in the specific example you posted and in this example here the second number contains a space between the minus sign and the number itself". I have just noticed that is not actuallt the case (it just looked like that in my newsreader) and there is no space between the minus sign and the number. However, what I have said about determing the exact format of the file data you are starting with still stands, and the example of "a number with a space after the minus sign" is just one example of the things you need to look out for in such situations where you are dealing with something other than comma separated or other standard separated values. Anyway, the ListBox code I posted will tell you exactly what you are dealing with. Mike
From: Jeff Johnson on 21 Apr 2010 09:36
"IJALAB" <balaji.draj(a)gmail.com> wrote in message news:d33cadbf-a920-43a2-b9fe-f2e06f77bfbe(a)o15g2000pra.googlegroups.com... For reference, the correct word is "question," not "doubt." I see this mistake a lot. |