Results 1 to 6 of 6

Thread: Question regarding files and OR

  1. #1

    Question regarding files and OR

    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.)

  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    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 sLines() AS STRING
    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
    Your:
    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
    NEXT
    
    if stateFound then
      ' -- Do something
    end if
    

    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  3. #3
    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.

  4. #4
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    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 1.8.6.0 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


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  5. #5
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10
    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
    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
    Last edited by ErosOlmi; 09-02-2011 at 22:13.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  6. #6
    Thanks guys.

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

    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.

Similar Threads

  1. ZIP files
    By Henry in forum File
    Replies: 4
    Last Post: 29-06-2011, 23:46
  2. help files
    By newguy1 in forum Shout Box Area
    Replies: 5
    Last Post: 29-03-2006, 11:26

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •