text data

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
sid6.7
Veteran
Posts: 318
Joined: Tue Jun 21, 2005 8:51 am
Location: west USA
Contact:

text data

Post by sid6.7 »

okay i'm probably trying to hard and cant think anymore.. :(

my random map maker for the 3D bzflag game is coming along fine
and works 99% of the time without a hitch....

problem is i have to hardcode all the shapes if i want more
then just the generic box and pyramids...so someone suggested
i have an importer....

so far though i only have a primitve one that is really not
much good...

BZEDIT is a 3D map maker that you can move rotate etc
objects and build a map file...which is a plain text file with
the following in it...


box
position x y z
rotation 0 to 360
size x y z
end

pyramid
.
.
same data
.
.
end

thats how BZedit or someone with NOTEPAD would make
the game objects...

my job would be to read that and place it into the random map
randomly...using an import feature...

but a normal data file puts data on one line x,y,z,b,c,d

this one puts data on seperate lines and different info on each line
and so far i can't seem to sepereate each lines data...

IE

position x y z

i need to seperate it cuase i will be changing the x y z mainly

right now i can only read the line and slap the WHOLE line
into the map file a straight read write thing...but can't change
the x y z...

i also can't figure how to read 1 line which may have only 1 item
VS the next line which would have 5 items...

my head hurts....:)

any ideas would be appreciated...
MystikShadows
Veteran
Posts: 703
Joined: Sun Nov 14, 2004 7:36 am
Contact:

Post by MystikShadows »

Hi sid :-).

ok, here's two functions that I created for my FBToolbox that might come in handy for you. GetWordCount() will tell you how many words are in a string and GetWordNumber will give you any word number you want from the string.

Code: Select all

' ========================================================
'  NAME........: GetWordCount()
'  PARAMETERS..: WorkString AS STRING
'  RETURN......: 0 Or the number of words
'  ASSUMES.....: WorkString is valid string with contents
'  CALLED FROM.: Anywhere
' --------------------------------------------------------
'  DESCRIPTION.: This function accepts a string as a
'                parameter, peruses it and accumulates
'                the number of words it finds in the
'                string.
' ========================================================
FUNCTION GetWordCount(WorkString AS STRING) AS INTEGER

    ' ----------------
    '  Work Variables
    ' ----------------
    DIM Counter      AS INTEGER
    DIM WordCounter  AS INTEGER
    DIM Position     AS INTEGER
 
    ' ----------------------
    '  Initialize Variables
    ' ----------------------
    WordCounter  = 1
    Position     = 1
    ' ---------------------------------------------------------
    '  First we put ourselves at the beginning of the 1st word
    ' ---------------------------------------------------------
    IF MID$(WorkString, Position, 1) = " " THEN
       DO WHILE MID$(WorkString, Position, 1) = " "
          Position = Position + 1
       LOOP
    END IF
    ' --------------------------------------
    '  Then we start the Word Count Process
    ' --------------------------------------
    FOR Counter = Position TO LEN(WorkString)
        IF MID$(WorkString, Counter, 1) = " " THEN
           WordCounter = WordCounter + 1
           DO WHILE MID$(WorkString, Counter, 1) = " "
              Counter = Counter + 1
           LOOP
        END IF
    NEXT Counter
    ' ----------------------------------------
    '  Return the number of words accumulated
    ' ----------------------------------------
    GetWordCount = WordCounter

END FUNCTION

' ========================================================
'  NAME........: GetWordNumber()
'  PARAMETERS..: WorkString AS STRING
'                WordNumber AS LONG
'  RETURN......: "" Or the desired word Number
'  ASSUMES.....: WorkString has contents, WordNumber > 0
'  CALLED FROM.: Anywhere
' --------------------------------------------------------
'  DESCRIPTION.: This function accepts a string and a
'                Word Number as paremeters, it then finds
'                the given word number and returns it to
'                the calling function.
' ========================================================
FUNCTION GetWordNumber(WorkString AS STRING, WordNumber AS LONG) AS STRING

    ' ----------------
    '  Work Variables
    ' ----------------
    DIM Counter      AS INTEGER
    DIM WordCounter  AS INTEGER
    DIM Position     AS INTEGER
    DIM ReturnString AS STRING

    ' ----------------------
    '  Initialize Variables
    ' ----------------------
    WordCounter  = 1
    Position     = 1
    ReturnString = ""
    ' ---------------------------------------------------------
    '  First we put ourselves at the beginning of the 1st word
    ' ---------------------------------------------------------
    IF MID$(WorkString, Position, 1) = " " THEN
       DO WHILE MID$(WorkString, Position, 1) = " "
          Position = Position + 1
       LOOP
    END IF
    ' --------------------------------------
    '  Then we start the Word Count Process
    ' --------------------------------------
    FOR Counter = Position TO LEN(WorkString)
        IF MID$(WorkString, Counter, 1) = " " THEN
           WordCounter = WordCounter + 1
           DO WHILE MID$(WorkString, Counter, 1) = " "
              Counter = Counter + 1
           LOOP
        END IF
        ' ----------------------------------------------------------
        '  If it's the wanted word number we put it in ReturnString
        ' ----------------------------------------------------------
        IF WordCounter = WordNumber THEN
           IF MID$(WorkString, Counter, 1) <> " " THEN
              ReturnString = ReturnString + MID$(WorkString, Counter, 1)
           END IF
        END IF
    NEXT Counter
    ' ---------------------------------
    '  Return the word in ReturnString
    ' ---------------------------------
    GetWordNumber = ReturnString

END FUNCTION
now whether you are reading:

position x y z a b c OR
position x y z

it won't change a thing...you can do something like:

Code: Select all

DIM WorkX AS INTEGER
DIM WorkY AS INTEGER
DIM WorkZ AS INTEGER
DIM TempString AS STRING 

' here is your line input statement to get the line
LINE INPUT #1, WorkString
IF UCASE$(GetWordNumber(WorkString, 1)) = "POSITION" then
    WorkX = VAL(GetWordNumber(WorkString, 2))
    WorkY = VAL(GetWordNumber(WorkString, 3))
    WorkZ = VAL(GetWordNumber(WorkString, 4))
    ' Ok now we want to change these 3, for example:
    WorkX = 100        
    WorkY = 200        
    WorkZ = 300        
    ' We then reformulate the string:
    TempString = "position " + TRIM$(WorkX) + " " + _
                               TRIM$(WorkY) + " " + _
                               TRIM$(WorkZ)
Now you have tempstring that has the new values..and you write that in the new file :-).


hope this helps.
Last edited by MystikShadows on Mon Nov 21, 2005 8:23 pm, edited 1 time in total.
When God created light, so too was born, the first Shadow!

MystikShadows

Need hosting? http://www.jc-hosting.net

Interested in Text & ASCII development? Look no further!
http://www.ascii-world.com
Guest

Post by Guest »

very cool thanks mystik...its almost working now...

1 more step.....


okay the line POSITION X Y Z


position is always the same in size 8 letters i can grab that okay...
the x y can fluctuate though in size...z i can grab from the right okay
its the middle ones x and y that are the problem...

x y and z can have any value from 0 to 999
how can i use left/right/mid to grab something that
changes in size from 1 space to 3 spaces...

1st example: position 0 2 4
position is 8 spaces
then 1 blank space
0 is 1 space
then 1 blank space
2 is 1 space
then 1 blank space
4 is 1 space...
then 1 trailing space

2nd item: position 10 111 1

blank space
now x is 2 space
blank
now y is 3 spaces
blank
now z is 1 space

3rd item: position -123 1 77

blank
now x is 4 spaces
blank
now y is 1 space
blank
now z is 2 space...

it keeps moving around
so a grab with left/right/mid wont work
cause i have to use a static number with left/right/mid

been looking at it for hours now and can't seem
to grab it...
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Wow, Mystik, your GetWordCount function is a veritable coding work of art! :D A function like this is the kind that you can read and understand, and you wouldn't hesitate to include in your program.

You should write a tutorial on recommended standards for coding with formats, comments, indentation, upper and lower case usage, etc.
*****
MystikShadows
Veteran
Posts: 703
Joined: Sun Nov 14, 2004 7:36 am
Contact:

Post by MystikShadows »

LOL thanks Moneo, coming from you, it's quite the compliment :-). I do my best to have all my code coded this way. Of course deadlines and comments are mutually exclusive as you know. But atleast I do always use very descriptive variables. :-).

Actually in part 3 of my GUI series, I define all my coding standard, code documentation and naming conventions in detail since I will be following it throughout the development. Look for it in the next QBE :-). Once it's out though I should isolate that part into a single article and add it to my website....:-).
When God created light, so too was born, the first Shadow!

MystikShadows

Need hosting? http://www.jc-hosting.net

Interested in Text & ASCII development? Look no further!
http://www.ascii-world.com
MystikShadows
Veteran
Posts: 703
Joined: Sun Nov 14, 2004 7:36 am
Contact:

Post by MystikShadows »

Anonymous wrote:very cool thanks mystik...its almost working now...

1 more step.....


okay the line POSITION X Y Z


position is always the same in size 8 letters i can grab that okay...
the x y can fluctuate though in size...z i can grab from the right okay
its the middle ones x and y that are the problem...

x y and z can have any value from 0 to 999
how can i use left/right/mid to grab something that
changes in size from 1 space to 3 spaces...

1st example: position 0 2 4
position is 8 spaces
then 1 blank space
0 is 1 space
then 1 blank space
2 is 1 space
then 1 blank space
4 is 1 space...
then 1 trailing space

2nd item: position 10 111 1

blank space
now x is 2 space
blank
now y is 3 spaces
blank
now z is 1 space

3rd item: position -123 1 77

blank
now x is 4 spaces
blank
now y is 1 space
blank
now z is 2 space...

it keeps moving around
so a grab with left/right/mid wont work
cause i have to use a static number with left/right/mid

been looking at it for hours now and can't seem
to grab it...
The GetWordNumber gets which ever word (or number) whever in the string it may be, no matter what length it is in character or digits. That shouldn't be a problem :-). you can also see how I define the size to get in the code to the GetWordNumber function. Basically I start at the first character (or numeric digit) and concatenate it until I read a space, then I know I have the whole numeric value.
When God created light, so too was born, the first Shadow!

MystikShadows

Need hosting? http://www.jc-hosting.net

Interested in Text & ASCII development? Look no further!
http://www.ascii-world.com
Post Reply