Menu

Show posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Show posts Menu

Messages - CW

#151
The bank system looks really interesting. (New Toy!) I'll have to give it some study.

Some of what Hello Kitty is doing there, efficiency wise, might come in handy for a very-long-number calculator project I've been thinking on.  (I have a simple Mandelbrot set generator I would like to expand on; and, for kicks, I would like to do a program to calculate lots of digits of pi. Twelve digits of accuracy in calculations can only get you so far, so what I really need to do is to write a very-long-number calculator.)
So many projects, so little time.  =D

Thanks Marmor! You are all right.  :booze:

-CW
#152
Well, SHUT MY MOUTH!

There is a Replace$() command! I'm sorry Marmor. I completely misunderstood what you were asking.
You have been very nice, but now you can just ignore me.

Here I've been replacing text the hard way all this time.  :S
Now if they can only perfect the DoWhatIMean() command, then I would be set.
LOL. Ah well. At least I learned something new today. 

Cheers!
-CW
#153
Hi Marmor.

There isn't a command which will replace chars within a string, so you will have to write your own. The commands from my first example will help you do the job.

As for the name of the function, you mentioned liking Replace() or Mid$(). Other languages allow programers to repeat the names of functions, a practice called overloading (which can be quite handy), but GLbasic doesn't allow that, so far as I know. Mid$() is used by GLbasic, so you won't be able to use it. But you can certainly make a function called Replace(), which will do exactly what you want.

You are right about how you will have to cut and past to get the output you need. Sometimes there is no alternative but to roll up your sleeves, jump right in and use some brute force. I suspect that may be the case with my own problem which I posted to this board. If GLbasic doesn't have a command for a thing, you have no choice but to fall back on your own programing which, written in BASIC, can be very slow compared to C++ or other more native level languages. Anyway, I'm rambling.

Just yell if you need a hand.  :good:
-CW
#154
You have the right idea, but you are using mid$() incorrectly. The proper usage  is like this, Ch$=mid$(Ok$,2,1)
which will return the third character in the Ok$ string. (The second char, actually, counting from zero.)
But if you are looking to replace one character or one string with another, that particular command can't do the job on its own.
I wrote a couple quick programs, one of which may be what you are after.
The first one simply replaces one char with another.

Code (glbasic) Select

String$ = "mammmmmmaaaammmm" // Let's replace every "a" with "-"

LookFor$ = "a";ReplaceWith$="-"
NewString$ = ""

FOR counter = 0 TO LEN(String$)
hold$ = MID$(String$,counter,1)
IF hold$ = LookFor$
NewString$ = NewString$ + ReplaceWith$
ELSE
NewString$ = NewString$ + hold$
ENDIF
NEXT

PRINT String$,10,10
PRINT NewString$,10,30
SHOWSCREEN;KEYWAIT;END



Or is the next function more of what you are looking for? It replaces entire strings of characters (full words).

I put all of the string-handling on a single line for compactness, to minimize variables needed and to optimize run time.
(This practice produces hard to read, debug or modify code and is generally frowned upon.)
If you would like the steps broken down so you can study them, just let me know and I be happy to do it for you.

Happy Coding!
-CW

Code (glbasic) Select

// (Excuse the mousefont. I am trying to keep things simple. Please add a font for readability if you wish it.)

String$ = "I love my dog. My dog is the best! But when I call my dog, my dog ignores me."
//Let's replace every 'dog' with a 'cat'. (Because cats rule, and dogs drool!) =^..^=

PRINT String$,1,1
ReplaceString("dog",String$,"cat") // If you hover over the function, you will see the syntax.
PRINT String$,1,20

SHOWSCREEN
KEYWAIT
END
///////////////////////////////////////////////    REPLACESTRING     ///////////////////////////////////////////////////////////
//                                                    Overview:
// This function finds all instances of Find$ embedded in a larger string (In_String$), and replaces them all with Replace_With$
// Process: The active line looks for the first instance of Find$, takes the text to the left of it, adds the replacement string,
// and then adds all of the text found to the right of it. (Find$ text is omitted from the new string.)
// The newly reconstructed string is then assigned back to the original String$ and the process repeats until all instances of
// Find$ have been replaced. Note that this function is case-sensitive. Use UCASE$() or LCASE$() if you want both caps and
// lower-case versions of the target text changed.
//
// Note that a function can only return a single value, so to deal with an entire string you either have to use a GLOBAL variable,
// or, better yet, do a BYREF, which creates a pointer to the ORIGINAL string. The original string gets changed.
// "In_String$" is the name of the pointer. It is not a variable, but merely points at the original variable in memory that you
//  sent to the function. You can think of the pointer as a second name for the same variable. That's not quite right, but it's close
// close enough. In reality, the variable has a unique name, the pointer has a unique name, and the pointer points to the variable.
// However you think about it, don't confuse pointers with variables. They are very different tools.
//
FUNCTION ReplaceString:Find$,BYREF In_String$,Replace_With$

WHILE INSTR(In_String$,Find$,0) > -1 // Note: a -1 means the INSTR() command didn't find any instances of Find$, so we should exit.

String$=LEFT$(In_String$,INSTR(In_String$,Find$,0))+Replace_With$+RIGHT$(In_String$,(LEN(In_String$)-LEN(Find$)-INSTR(In_String$,Find$,0)))

WEND

ENDFUNCTION
#155
Hi, I've enjoyed reading the posts here in times past, but this is the first time I have needed advice. I hope someone can help.

I am currently well into the writing of an animation spread-sheet program, which treats animation frames and operations on them similarly to how spreadsheet functions operate on data cells. Each row of my spreadsheet contains a different filmstrip, each with a different layer or object for the composite final animation. All rows will be stacked on top of each other, like layers, for the final animation. You get the idea. So far so good.

Needless to say, I have a LOT of frames. I calculate that my memory will allow six filmstrips of 130 frames each, plus the final composite film strip, for a total of 910 frames. So far I have been crunching data using arrays extensively, and then copying individual frames to a workspace one-dimensional array from which to make a sprite to draw onto the spreadsheet cell. That is a lot of copying going on. Can it be avoided?

It occurs to me that the program would run much faster if I can make better use of the native GLbasic sprite handling functions. For example, SPRCOLL() could be used to detect blank frames for the purpose of setting the end pointers or detecting empty film tracks after the user deletes one or more frames. But to use the sprite handling functions, I need to use MEM2SPRITE(). Here we get to the horns of my dilemma.

With so many frames contained within so many filmstrip rows, each with a full sprite of data, it would be very difficult to manage lots and lots of single-dimensional arrays; yet MEM2SPRITE() seems to function only with single dimensional arrays. Is there a way to eat my cake and have it too?

I wrote the following bit of code to show the two approaches I have taken in trying to program around this limitation. Neither approach seems to work, but maybe I am missing something. That is what I would like advice on, please.

For the record, I am just a hobbyist when it comes to programing. I can manage most tasks I set myself with BASIC, but I am a rank amateur at C++, so I would like to stick with basic if at all possible.

Thanks for any advice you can give. Here is my demonstration code. -CW
Code (glbasic) Select

// Attempting MEM2SPRITE() work around.
// (Excuse the mousefont. I am trying to keep things simple. Please add a font for readability if you wish it.)

TYPE Track
GraphicData[10000] //100 X 100 sized frames (or sprites)
ENDTYPE

GLOBAL Filmstrip%[] AS Track
DIM Filmstrip[6]
// Note that for simplicity I have removed an entire hierarchy layer of complication (IE: 6 tracks, each 130 frames long,
// each frame containing an entire sprite worth of pixel data.) The simplified model is sufficient for the problem I am trying to solve.

LOCAL counter% = 0 // Count number of pixels set for later print statement.
FOREACH frame IN Filmstrip[];FOREACH pixel IN frame.GraphicData[];pixel = RGB(255,255,0);counter=counter+1;NEXT;NEXT

PRINT "All Frames in all 6 filmstrips filled with yellow. ",10,30
PRINT counter+" Pixels set.",10,60
PRINT "Attempting to create a sprite using a pointer in MEM2SPRITE(). PRESS KEY.",10,90
SHOWSCREEN;KEYWAIT

//Let's create a comparison sprite from a single-dimensional array to demonstrate that the mechanics are otherwise sound.
GLOBAL CS%[]
DIM CS%[10000]
FOREACH pixel IN CS[];pixel=RGB(0,255,0);NEXT
MEM2SPRITE(CS[],2,100,100)

////////////////////////////////////////////////////////
// This next bit is the point of the program.
// I am trying to get around the single dimensional array limitation of MEM2SPRITE() using a pointer.
// (I know, I know, arrays all pass pointers anyway, so why should MEM2SPRITE be so picky on this?
// Instead of a pointer to a single array, why not a pointer to a single array within a multidimensional array? Can it be done?)

//MEM2SPRITE(Filmstrip[2].GraphicData[],1,100,100) //The direct approach

ALIAS FramePointer AS Filmstrip[2]
// MEM2SPRITE(FramePointer.GraphicData[],1,100,100)   //Using a pointer as stand-in for Filmstrip[2], just in case MEM2SPRITE is syntax sensitive, for some reason.
//
// Neither approach will compile successfully. It seems GLbasic can't handle this, or am I missing something?
////////////////////////////////////////////////////////

CLEARSCREEN
ALPHATESTING 1 // Disable transparency, as I did not set Alpha when I filled the frames with color.
DRAWSPRITE 1,70,10;DRAWSPRITE 2,400,10
PRINT "Multi-Array Sprite",50,120;PRINT "Single-Array Sprite",375,120
PRINT "Press Key to Exit",220,150;SHOWSCREEN;KEYWAIT
END