search more than 2 items
Post by hammerjit on Jul 23rd, 2015, 09:03am
Hi....I am having problem with this combining 3 conditions
I want to search my data file for '180822' and lines without "CF".
But I cant get it to work.
This is my sample data
CF,2015,180821,5,24
CF,2015,180822,2,21
CF,2015,180823,7,24
CF,2015,180824,1,16
1,2015,180821,24-07-15,1
1,2015,180822,24-07-15,1
1,2015,180803,15-07-15,1
4,2015,180804,20-07-2015,1
1,2015,180822,25-05-15,0.5
This is my code
Code:
open "test.txt" for input as #fo
search1$="180822"
c=0
line input #fo,l$
while eof(#fo)=0 or (trim$(word$(l$,3,","))=search1$ and word$(l$,1,",")<>"CF")
c=c+1
input #fo, q$
test1$(c)=q$
input #fo, q$
test2$(c)=q$
input #fo, q$
test3$(c)=q$
input #fo, q$
test4$(c)=q$
input #fo, q$
test5$(c)=q$
end if
wend
close #fo
recnum=c
showRec1$=""
showRec2$=""
for i=1 to recnum
showRec1$=showRec1$+test4$(i)+chr$(13)+chr$(10)
showRec2$=showRec2$+test5$(i)+chr$(13)+chr$(10)
next i
print showRec1$
print showRec2$
Re: search more than 2 items
Post by Richard Russell on Jul 23rd, 2015, 10:38am
on Jul 23rd, 2015, 09:03am, hammerjit wrote:I want to search my data file for '180822' and lines without "CF". |
|
If the extract from your data file is typical, it seems that you don't actually need to test the individual fields separately because - apparently - '180822' can only occur in the 3rd field and 'CF' only in the 1st field. In that case you can check the entire line in one go using INSTR:
Code: open "test.txt" for input as #fo
while not(eof(#fo))
line input #fo, l$
if instr(l$, "180822") <> 0 and instr(l$, "CF") = 0 then
print l$
end if
wend
close #fo
end
But if it's important to check the specific fields then this works:
Code: open "test.txt" for input as #fo
while not(eof(#fo))
line input #fo, l$
if word$(l$, 3, ",") = "180822" and word$(l$, 1, ",") <> "CF" then
print l$
end if
wend
close #fo
end
I've not bothered to store the results in an array for later use, but if you need that it is obviously simple to add:
Code: open "test.txt" for input as #fo
c = 0
while not(eof(#fo))
line input #fo, l$
if word$(l$, 3, ",") = "180822" and word$(l$, 1, ",") <> "CF" then
c = c + 1
test$(c) = l$
end if
wend
close #fo
recnum = c
showRec1$ = ""
showRec2$ = ""
for i = 1 to recnum
showRec1$ = showRec1$ + word$(test$(i), 4, ",") + chr$(13) + chr$(10)
showRec2$ = showRec2$ + word$(test$(i), 5, ",") + chr$(13) + chr$(10)
next i
print showRec1$
print showRec2$
Re: search more than 2 items
Post by hammerjit on Jul 27th, 2015, 02:42am
Thanks Richard...that helped me a lot.
Re: search more than 2 items
Post by hammerjit on Jul 27th, 2015, 08:57am
Richard, just to add to this code...if I want to output but after sorting the date field(4 field) how can I go about that?
eg. the above code will output
180822 24-07-15
180822 25-05-15
But it should be sorted by May then July.
CF,2015,180821,5,24
CF,2015,180822,2,21
CF,2015,180823,7,24
CF,2015,180824,1,16
1,2015,180821,24-07-15,1
1,2015,180822,24-07-15,1
1,2015,180803,15-07-15,1
4,2015,180804,20-07-2015,1
1,2015,180822,25-05-15,0.5
Code:
open "test.txt" for input as #fo
c = 0
while not(eof(#fo))
line input #fo, l$
if word$(l$, 3, ",") = "180822" and word$(l$, 1, ",") <> "CF" then
c = c + 1
test$(c) = l$
end if
wend
close #fo
recnum = c
showRec1$ = ""
showRec2$ = ""
for i = 1 to recnum
showRec1$ = showRec1$ + word$(test$(i), 4, ",") + chr$(13) + chr$(10)
showRec2$ = showRec2$ + word$(test$(i), 5, ",") + chr$(13) + chr$(10)
next i
print showRec1$
print showRec2$