Click here to LOGIN
Information
Tutorials & Articles
Programs
Feedback
 


Forum
Add a Post
Username:   (You must log on to use your member username)Hint: Use [code] and [/code] around text to highlight it as QB code.


Subject:
Message:
Forums -> Q & A -> Advanced INSTR help
sintral
Advanced INSTR help
Posted Jul 08 2009
I'm reading in a text file line by line, parsing any arbitrary commas and replacing them with spaces using INSTR (i.e. Company,Inc. = Company Inc.); no problems there.

In cases where commas are used in decimals or integers (i.e. 7,345.45 or 10,453) I would like to simply remove the comma. In C++ and other languages I know there are simple functions to check for letters and numbers, is there a way to do it in QBasic?

I have something like this in mind:


com = INSTR(1, row$, [number]; ","; [number])
IF com > 0 THEN
    row$ = LEFT$(row$, com - 1) + MID$(row$, com + 1)
END IF



Where [number] is replaced by the functionality that I'm looking for.
Clippy
You CAN'T use number values with commas in them in QB!
Posted Jul 08 2009
So they don't belong there anyhow!
sintral
I may have been unclear
Posted Jul 09 2009
INSTR needs to return the position of the first instance of a one digit integer, a comma, and a one digit integer; in that order.

In the example I gave above, 7,345.45, INSTR should return the position of 7,3 since it meets the criteria I'm looking for.

I can't control the input file, so to say that commas are not allowed in numbers is neither here nor there. Besides, as you can see the purpose is to locate and remove the comma.
09cOdE
Here is a thought
Posted Jul 09 2009
open "mytextfile1.txt" for Input as #1
open "mytextfile2.txt" for output as #2
do while not EOF(1)
input #1,text$
if text$="," then text$="."
write#2,text$
loop
close #1
Close #2
sintral
reply to 09cOdE
Posted Jul 09 2009
I believe that loop will simply replace all commas with periods. Commas not occurring between two integers will need to be replaced with a space, commas occurring between two integers need to be removed (not replaced by any character).
Clippy
You cannot INPUT # strings with commas!
Posted Jul 09 2009
INPUT # stops at ANY comma so you lose part of the string. The next INPUT will grab the rest.

USE LINE INPUT # with commas in strings to get the entire string.

You can use INSTR in a loop to find all of the commas.


start = 1
DO
location = INSTR(start, oldstring$, ",")
IF location THEN
   newstring$ = newstring$ + MID$(oldstring$, start, location - 1)
ELSE : newstring$ = newstring$ + MID$(oldstring$, start, (LEN(oldstring$) - start) + 1 : EXIT DO
start = location + 1
LOOP

Clippy
OOPS try this in line 5. It gets complicated
Posted Jul 09 2009
newstring$ = newstring$ + MID$(oldstring$, start, ((location - 1) - start) + 1)
Clippy
Simpler way than INST
Posted Jul 09 2009
Assuming a string number is used:


L = LEN(number$)
FOR n = 1 to L
    Tmp$ = MID$(number$, n, 1)
    IF Tmp$ <> "," THEN NewNum$ = NewNum$ + Tmp$
NEXT

Clippy
BTW if you are reading from a file..........
Posted Jul 09 2009
LINE INPUT #, number$ 'returns a string number plus anything else in the file's line.

You can use VAL without the commas to convert it back to a real number.
asswipe
asswipe
Posted Jul 11 2009
'inp.txt is the file with the commas to remove
'out.txt is the file created that contains the file with commas to remove without the commas according to your instructions on comma removal
DIM ass AS STRING * 1, dick AS STRING * 1, l AS STRING * 1, r AS STRING * 1, i AS LONG
dick = SPACE$(1)
OPEN "inp.txt" FOR BINARY AS #1
OPEN "out.txt" FOR BINARY AS #2
7 i = i + 1
GET #1, i, ass
IF ass <> "," THEN 8 ELSE IF i = 1 THEN 1
GET #1, i - 1, l
GET #1, i + 1, r
IF ASC(l) > 47 AND ASC(l) < 58 AND ASC(r) > 47 AND ASC(r) < 58 THEN 7
1 PUT #2, , dick
GOTO 7
8 PUT #2, , ass
IF NOT EOF(1) THEN 7
CLOSE
SYSTEM
'Enjoy
Clippy
Hmmmmm
Posted Jul 11 2009
Never thought of Binary mode! But asswipe you would remove all of the commas from a WRITE created file so that INPUT # cannot be used anyhow! I guess that's how you got the name :-P
09cOdE
Geeesh
Posted Jul 13 2009
What an ass
stevex
Instr help
Posted Jul 16 2009
  Instr only searches for a substring such as John Smith, or Joe,s mousetrap factory.
   Also, there is an working example in your Qbasic help file
stevex

The QBasic Station, (C) Copyright 1997-2010