line input problem
line input problem
line input problem
line input problem
line input problem
line input problem line input problem line input problem line input problem line input problem line input problem line input problem line input problem
line input problem line input problem
line input problem
Go Back  Xtreme Visual Basic Talk > > > line input problem


Reply
 
Thread Tools Display Modes
  #1  
Old 12-19-2017, 12:22 PM
starmanMike starmanMike is offline
Junior Contributor
 
Join Date: Oct 2005
Location: near Norwich, UK
Posts: 310
Default line input problem


Hi,
Having thought this problem had been solved, it's reaered its ugly head again (or is it just me rearing my ugly head? ;-) )
I have a text file delimited by " ## " - that's space, hash+hash, space. So I want to make an array using split() as follows (all variables have been previously declared and are OK, I have checked):
Code:
Open thefile For Input As filenum
While Not EOF(filenum)
Line Input #filenum, textofline
'Put here the code you want to do with the previous line
Debug.Print textofline
Now - if I do this the whole file in bold above writes perfectly to the immediate window - col headings and first few lines here:-
Type ## StarName ## AUID ## JD ## Fainter ## Mag ## Comment Codes ## Uncertain ## Filter ## Mag Error ## Comp Label ## Comp Mag ## Check Label ## Check Mag ## ChartID ## MagType

Vis ## WW VUL ## 000-BCG-736 ## 2458010.37 ## 0 ## 10.7 ## ## 0 ## Vis. ## ## 105 ## ## ## ## 13473CST ## STD
Vis ## AV CYG ## 000-BCG-354 ## 2458010.37 ## 0 ## 10.4 ## ## 0 ## Vis. ## ## 102 ## ## ## ## X18451DPL ## STD
Vis ## AE AQR ## 000-BCM-111 ## 2458010.37 ## 0 ## 11.5 ## ## 0 ## Vis. ## ## 114 ## ## ## ## 1952 ## STD
Vis ## RX AND ## 000-BBC-342 ## 2458010.37 ## 0 ## 12.1 ## ## 0 ## Vis. ## ## 118 ## ## 122 ## ## 5976PI ## STD
So far, so good. However, when I use the split function instead to extract just the contents of 'textofline':
Code:
bits = Split(textofline, " ## ")
   sname = Trim(bits(1))
   '(other variables declared similarly for some other 'bits'...)
debug.print sname '(and other 'bits')...
Wend
Close filenum
All I get echoed are the column headings from the first line (which I don't want anyway!) and nothing else! What am I doing wrong?
Reply With Quote
  #2  
Old 12-20-2017, 04:20 AM
Daigon Ali Daigon Ali is offline
Regular
 
Join Date: Aug 2014
Location: London
Posts: 66
Default

Hi,

In your text file you have a blank line after the headings. This doesn't contain any delimiter, so your reference to bits(1) should throw an error since there is no element 1 for the blank line.

You could solve this by checking for the presence of the delimiter in textofline before performing the split and other operations etc.

To ignore the headings, declare a temporary variable tmp and add
Code:
If Not EOF(1) Then Line Input #1, tmp
after opening the file and before the While.
Reply With Quote
  #3  
Old 12-20-2017, 09:31 AM
starmanMike starmanMike is offline
Junior Contributor
 
Join Date: Oct 2005
Location: near Norwich, UK
Posts: 310
Default

Thanks loads! I never noticed this at all. Good to have not only an expert but a fresh pair of eyes. So is there any way to ignore not only the top line but the blank line as well - i.e., start at 'line 3'? (the top line is actually not a huge problem - I already have a mini-sub that deletes that after it's been stored in a DB, which is where all the required bits from 'bits' will end up) - but I basically do an 'instr' search for the delimiter?
Reply With Quote
  #4  
Old 12-20-2017, 01:06 PM
Daigon Ali Daigon Ali is offline
Regular
 
Join Date: Aug 2014
Location: London
Posts: 66
Default

You're very welcome.

I think it's probably easiest to ignore the blank line.
You could just test its length i.e. if Len(textofline)>0

Or you could, as you say, use instr to see if a delimiter is present. e.g.
If instr(textofline, " ## ")>0

Also, since you're specifying the elements for "bits", you could check that the correct number of delimiters (and therefore "bits" elements you refer to) are present (and then ignore or raise an error).
The number of delimiters is the length of textofline minus its length without the delimiters.

So if my delimiter is a # then the calculation of the number of # for s1 and s2 is 3.
Code:
Private Sub Command1_Click()
s1 = "a#b#c#d"
s2 = "a###d"

Debug.Print "The number of delimiters in S1 is " & Len(s1) - Len(Replace(s1, "#", ""))
Debug.Print "The number of delimiters in S2 is " & Len(s2) - Len(Replace(s2, "#", ""))

End Sub
Reply With Quote
  #5  
Old 12-20-2017, 02:33 PM
starmanMike starmanMike is offline
Junior Contributor
 
Join Date: Oct 2005
Location: near Norwich, UK
Posts: 310
Default problem solved!

OK! Solved the problem (I think) with your help and a bit of googling. Used FSOs instead, and will post the code later just in case anyone is having a similar problem. Thanks for pointing me in the right direction.

Last edited by starmanMike; 12-21-2017 at 06:51 AM. Reason: problem solved
Reply With Quote
Reply

Tags
vis, line, std, mag, bits, input, split, textofline, headings, check, label, comp, debug.print, filenum, variables, declared, ugly, file, sname, head, space, aqr, 000-bcg-354, x18451dpl, cyg


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
line input problem
line input problem
line input problem line input problem
line input problem
line input problem
line input problem line input problem line input problem line input problem line input problem line input problem line input problem
line input problem
line input problem
 
line input problem
line input problem
 
-->