View Full Version : Question regarding files and OR

08-02-2011, 01:09
Just have 2 more questions for today.

1. To open 1 file, I did this:
FileName = APP_ScriptPath + "test.txt" ' Build filename
FileHandle = FILE_Open (FileName, "OUTPUT")

If I wanted to open and write to 2 different files, how would I go about do this?

2. In the thinBasic help pdf, it said that the OR operator checks against 2 expressions. Can you check more than 2 expressions such as If x = 2 or 3 or 4 or 99 or 101 or 200 or 500 or 1000?

Thanks. Originally I was testing for the valid 50 states in the IF statement but it didn't work. (IF state = "AK" OR "AL" OR "AR" OR "AZ" etc. Didn't know about the underscore at that time for continuation.)

Petr Schreiber
08-02-2011, 11:00
Hi TheOne,

Add 1
If you handle two different files, you need to store two different handles:

Dim FileHandleA, FileHandleB As DWord
FileHandleA = FILE_Open (FileNameA, "OUTPUT")
FileHandleB = FILE_Open (FileNameB, "INPUT")

Note - you can also load whole file at once to string:

DIM sFileContent AS STRING

sFileContent = File_Load("c:\test.txt")

Or retrieve individual lines to string array directly:

DIM nLines AS LONG = PARSE( FILE "c:\test.txt", sLines, $CRLF)

... and write whole file from string:

DIM sFileContent AS STRING
sFileContent = "My text for file, line 1" + $CRLF + "Line 2"

File_Save("c:\test.txt", sFileContent)

Add 2

If x = 2 or 3 or 4 or 99 or 101 or 200 or 500 or 1000 then ...

is valid for numeric expressions, but it actually evaluates the part after = as expression.

What you need here is:

if x = 2 or x = 3 or x = 4 or x = 99 or x = 101 or x = 200 or x = 500 or x = 1000 then

or more straightforward test:

if In(x, 2, 3, 4, 99, 101, 200, 500, 1000) then

The In works just for numeric cases.

For string cases, if you test for 51 states which you have in array, I would do:

stateFound = false
for i = 1 to UBound(aStates) ' -- = from 1 to upper bound of aStates array, which is 51
if state = aStates(i) then
stateFound = true
exit for
end if

if stateFound then
' -- Do something
end if


08-02-2011, 19:21
Thanks again Petr.

After looking over the help manual, I saw that I would need 2 handles for the files. Sometimes more examples are useful.

You are the best. It's tricky learning a new language and all the syntax that goes with it. I wouldn't have known about the underscore for continuation.

Thanks also for the coding examples.

Petr Schreiber
08-02-2011, 20:53
Hi TheOne,

your input is very important for us, as we can tune up the help file according to input we get from you!

Regarding underscore - since it is not necessary, but I guess it will be worth some note in help to mention you can do multiline stunts, as it is not common in BASICs generally :)


09-02-2011, 08:02
Hi TheOne and welcome to thinBasic community forum.

Petr, thanks for the quick and precise reply.

For scanning inside a string array for an element, also consider ARRAY SCAN ... functionality (http://www.thinbasic.com/public/products/thinBasic/help/html/array_scan.htm)
so your FOR/NEXT loop for searching of a state can be substituted by something like:

lIndex = ARRAY SCAN aStates(), COLLATE UCASE, = ucase$(state)

that will quickly scan aState() array searching for state regardless case sensitivity.
If state has been found, lIndex will contain position (1 based) inside of aState array where state has been found, otherwise zero.

When I write help material think all (at least most) has been written but when people start to ask about details I realize that so little has been written :oops:

09-02-2011, 20:28
Thanks guys.

This forum is great for help for us "Basic dummies". :D

You provide a quick answer and also how the code may be used.
This helps me because examples give you an idea and once learned, it can be used over and over again.

I hope I wouldn't have to ask too many "dumb" questions. And maybe in the future I could help out others.

I'm working on a data entry program to create a comma or tab delimited file. So far it is working good and now I am adding edits. Just barely using the full richness of thinBasic yet as I don't need to use the other modules yet. But I will play with it later to educate myself.