AllExperts > Qbasic, Quickbasic 
Search      
Qbasic, Quickbasic
Volunteer
Answers to thousands of questions
 Home · More Qbasic, Quickbasic Questions · Answer Library  · Encyclopedia ·
More Qbasic, Quickbasic Answers
Question Library

Ask a question about Qbasic, Quickbasic
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Don Schullian
Expertise
I`ve worked in BASIC since the Commodore days and maintain a web site part of which is devoted to helping others with thier problems. I`m proficient in all versions of PowerBASIC but cross over to QuickBASIC and Qbasic frequently.

 
   

You are here:  Experts > Computing/Technology > Basic > Qbasic, Quickbasic > Quic Basic string manipulation

Qbasic, Quickbasic - Quic Basic string manipulation


Expert: Don Schullian - 2/24/2009

Question
QUESTION: Hi Don,
I am working in QB 4.5 and trying to manipulate a string that represents sample information
Below is an example string
"=e804f030,28-30cm,0.2987in,s2008256,ct2008300,15.317g,c2008270—"

I would like to remove all non alpha numeric characters except decimal, dash, and comma and then put each part of the string separated by a comma into its own string for expert

the above string would be transformed into the following:

s1$=e804f030
s2$=28-30cm
s3$=0.2987in
s4$=s2008256
s5$=ct2008300
s6$=15.317g
s7$=c2008270

Once the information was divided up, would it then be possible to remove the alpha characters on some of the individual strings? For example, I'd like to keep the alpha information in the first two new strings but remove it from the rest e.g.,
s7$=c2008270 becomes 2008270

Thanks in advance.

Best regards,

Tim



ANSWER: Hi Tim,

 The code below seems to work. I wrote routines like these decades ago but couldn't find the originals. So..... I think these work. The SUB ParseString will only work correctly when the Sep$ is one character in length.

 BTW, I don't have a DOS compiler loaded any more so you may have to fuss with the code a bit to get it to work in QB as I couldn't test it directly.

FUNCTION fRetain$ ( InString$, Keep$ )

 FOR P% = 1 TO LEN(InString$)
   C$ = MID$(InString$,P%,1)
   IF INSTR(Keep$,C$) > 0 THEN
     OutString$ = OutString$ + C$
   END IF
 NEXT

 fRetain$ = OutString$

END FUNCTION

FUNCTION fParseCount% ( InString$, Sep$ )

 L% = LEN(Sep$)
 P% = 1-L%

 DO
   C% = C% + 1
   P% = P% + L%
   P% = INSTR(P%,InString$, Sep$)
 LOOP UNTIL P% = 0

 fParseCount% = C%

END FUNCTION

SUB ParseString ( InString$, Sep$, Arr$() )

 Last% = UBOUND(Arr$())

 FOR C% = 1 TO Last%
   P1% = P1% + 1
   P2% = INSTR(P1%,InString$,Sep$)
   IF P2% = 0 THEN P2% = LEN(InString$) + 1
   Arr$(C%) = MID$(InString$,P1%,P2%-P1%)
   P1% = P2%
 NEXT

END SUB

N$   = ",=e804f030,28-30cm,0.2987in,s2008256,ct2008300,15.317g,c2008270—,"
K$   = "0123456789,-.abcdefghijklmnopqrstuvwxyz"
Sep$ = ","
N$   = fRetain$(N$,K$)
C%   = fParseCount%(N$,Sep$)
DIM S$(C%)
ParseString(N$,Sep$,S$())

PRINT N$
PRINT ""
WHILE C% > 0
  PRINT CHR$(34); S$(C%); CHR$(34)
  C% = C% - 1
WEND

---------- FOLLOW-UP ----------

QUESTION: Now that I can take the binary file and translate it into a 16398 row by two column text file, with everything where I want it,I want to do this for multiple file in the same directory and put the data in a single text file.

I came up with a routine to read all the names of the original data files and create individual text files automatically, but I'm not sure how to combine the files into a single text file. For example let's say I have four text files (2 x 16389), how would I open them and combine them into one file that is 8R x 16389C? Thanks again I really appreciate your help.

Answer
Hay Tim,

Ok, you've done the hard work! ;)

Lets say you've got your file names in an array F$() and the number of files in a variable Last%

OPEN "BIG_FILE.TXT" FOR OUTPUT AS #1
FOR L% = 1 TO Last%
 OPEN F$(L%) FOR BINARY AS #2
 ' Process your data here and
 ' PRINT #1, Whatever$
 CLOSE #2
LOOP
CLOSE #1

It should be about that easy.


Add to this Answer   Ask a Question


 
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.