Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

filling an array with lines of text 1

Status
Not open for further replies.

PureSoft

Programmer
Aug 12, 2002
24
GB
Hi,

I have a simple loop that seems to cause problems with 'subscript out of range' - I want to read in a text file, line by line and fill it into an array for subsequent reference. Here is the code:

m = 0
While Not EOF(1)
Line Input #1, Line_holder_array(m)
m = m + 1
Wend

What's my mistake?

thanks, Philip.
 
Hi Philip,

How have you declared Line_holder_array?

Dave
 
sorry, its defined as:

Dim Line_holder_array() As String
 
Try this then

m = 0
While Not EOF(1)
ReDim Preserve Line_holder_array(m)
Line Input #1, Line_holder_array(m)
m = m + 1
Wend
 
Or to be strictly accurate
m = 0
While Not EOF(1)
ReDim Preserve Line_holder_array(m) As String
Line Input #1, Line_holder_array(m)
m = m + 1
Wend


Dave
 
Thanks,

Thats gotten me a couple of lines further :) However I'm now having problems with assigning this captured line from the array to another array (filling it with the split up values form the original line of text), I'm getting type mismatch error for the last line shown:

Dim strStripped as String
Dim hold_values(), Line_holder_array () As string
.. ..
.. ..

m = 0
While Not EOF(1)
ReDim Preserve Line_holder_array(m) As String
Line Input #1, Line_holder_array(m)
m = m + 1
Wend

For j = LBound(Line_holder_array) To UBound(Line_holder_array)

strStripped = Replace(Line_holder_array(j), Chr(34), "")

hold_values = Split(strStripped, ",")
.. .. ..
.. .. ..

Once this extra bit is sorted out I should be fine, thanks for the pointer.

philip
 
The problem is that your array hold_values is being declared as a variant.
instead of
Dim hold_values(), Line_holder_array () As String
try
Dim hold_values() As String, Line_holder_array () As String

Dave
 
I just re-read the variable declaration help and see that variables on the same line, separated by commas, do not get the same type as the last defined, rather they get the default variant, unless specifically defined otherwise.

I didn't realise that was the case, thanks for the info, I'm on my way again.

Philip.
 
No problem.

Thanks for the star.

Good luck with the rest of it.

Dave
 
>they get the default variant, unless specifically defined otherwise

This has probably caught many of us at one point or another
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top