Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
filtering files
#1
okay i have a question on file output how would i filter somthing out like if it hade "a" in it then remove it from a file
Reply
#2
INSTR([start,]stringexpression1,stringexpression2)
_ stringexpression1 is the string to be searched
_ stringexpression2 is the string to look for
_ If the optional start argument is used to begin the search beyond the first character of stringexpression1, it must be a numeric-expression with an integer value between 1 and 32,767.

it searches a string for another string.
after that you need to use some logic to remove it

try using LEFT$ and RIGHT$ and LEN
[Image: sig.php]
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Reply
#3
MID$ might also come in handy. Big Grin
I'd knock on wood, but my desk is particle board.
Reply
#4
sure :roll:

i made a filter function that doesn't use MID =P
[Image: sig.php]
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Reply
#5
MID$ ???
could you elobrate
Reply
#6
Quote:okay i have a question on file output how would i filter somthing out like if it hade "a" in it then remove it from a file

Do you mean that the file's name has an "a" in it (as in "happy.txt")? Or do you mean that the data you are writing to the file has an "a" in it (for example, you are saying PRINT #1, s$ and you want to know if s$ has the character "a" within it)?

If you just want to find the letter "a" and replace it within a string (which will work for either case above), you can use the MID$ statement (not the function!) as such:
Code:
s$ = "happy joy joy whee yay"
apos = INSTR(s$, "a", 1)
WHILE apos <> 0
  MID$(s$, apos, 1) = "x"
  apos = INSTR(s$, "a", apos + 1)
WEND
Now s$ = "hxppy joy joy whee yxy".
If you want to do something more complicated such as totally removing the letter "a", you would probably be better off building up an entirely new string and using the MID$ function to iterate over each character in the original string and adding only the acceptable characters to the new string, like this:
Code:
s$ = "happy joy joy whee yay"
FOR i = 1 TO LEN(s$)
  char$ = MID$(s$, i, 1)
  IF char$ <> "a" THEN
    news$ = news$ + char$
  ELSE
    ' this is just for demonstration; to replace "a" with something else, put it between the quotes
    news$ = news$ + ""
  END IF
NEXT i
s$ = news$
Now s$ = "hppy joy joy whee yy". If you need to replace one string with a different string of a different size than the first string, you could also use something similar to the second example.
Reply
#7
DrV: yup but your example doesn't work with strings longer then one letter long. this works (though it's a function =P)

Code:
FUNCTION filter$ (text$, look$)
IF LEN(look$) = 0 THEN filter$ = text$: EXIT FUNCTION
DO
IF INSTR(text$, look$) <> 0 THEN
lefttmp$ = LEFT$(text$, INSTR(text$, look$) - 1)
righttmp$ = RIGHT$(text$, LEN(text$) - INSTR(text$, look$) - (LEN(look$) - 1))
text$ = lefttmp$ + righttmp$
END IF
LOOP UNTIL INSTR(text$, look$) = 0
filter$ = text$
END FUNCTION
[Image: sig.php]
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Reply
#8
Sum crazy code:

Code:
FILES *.*
INPUT "File To Sift: ", file$

OPEN file$ FOR INPUT AS #1
'Error check
IF LOF(1) < 1 THEN
PRINT "File [";ltrim$(rtrim$(file$));"] does not exsist!"
PRINT "Closing..."
CLOSE #1
PRINT "Good Bye"
END
END IF

PRINT "File successfully loaded!"
PRINT

PRINT "If text contains 'lookfor$', then sift."
INPUT "Sift from: ", lookfor$

REDIM Temp(1 to 1) AS STRING
REDIM Temp2(1 to 1) AS STRING

PRINT "Begining Parsing.....";

cntr = 0
DO UNTIL EOF(1)
cntr = cntr + 1

INPUT #1, strg$

IF INSTR(strg$, lookfor$) = 0 THEN

'A REDIM eraeses all previous data in array, so we have to back it
'up
REDIM Temp2(1 to cntr)

FOR i = 1 to cntr - 1
Temp2(i) = Temp(i)
NEXT i

REDIM Temp(1 to cntr)

FOR i = 1 to cntr - 1
Temp(i) = Temp2(i)
NEXT i

Temp(UBOUND(Temp)) = strg$

ELSE
cntr = cntr - 1
END IF
LOOP

PRINT "Done"

PRINT

PRINT "ReWriting File...";

max% = cntr

CLOSE #1
OPEN file$ FOR OUTPUT AS #1   'This clears the file, and opens it
                                                   'fresh
FOR i = 1 to max%
PRINT #1, Temp(i)
NEXT

Print "Done"

CLOSE #1

END
Reply
#9
a$ = MID$(b$, start%, end%)

a$ is the string expression in b$, starting @ start%, and continuing end% spaces

String$ = "This is an example"

a$ = MID$(String$, 5, 4)

a$ = "is a"
Reply
#10
my crazy code. seaches an entire file for a word and removes all traces.
Code:
DECLARE FUNCTION filter$ (text$, look$)
CLS
OPEN "filter.bas" FOR INPUT AS #1
OPEN "filter2.bas" FOR OUTPUT AS #2
DO
INPUT #1, a$
PRINT #2, filter$(a$, "filter")
LOOP UNTIL EOF(1)
CLOSE

FUNCTION filter$ (text$, look$)
IF LEN(look$) = 0 THEN filter$ = text$: EXIT FUNCTION
DO
IF INSTR(text$, look$) <> 0 THEN
lefttmp$ = LEFT$(text$, INSTR(text$, look$) - 1)
righttmp$ = RIGHT$(text$, LEN(text$) - INSTR(text$, look$) - (LEN(look$) - 1))
text$ = lefttmp$ + righttmp$
END IF
LOOP UNTIL INSTR(text$, look$) = 0
filter$ = text$
END FUNCTION
[Image: sig.php]
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)