GLBasic forum

Main forum => GLBasic - en => Topic started by: Sokurah on 2010-Sep-29

Title: Question about string handling.
Post by: Sokurah on 2010-Sep-29
In BMax I normally use this when printing the score on the screen;

DRAWTEXT ("score: " + Replace:String(RSet:String(Score, 7), " ", "0"), 1, 1)

This example will print the score, aligned to the right, and padded with zeros on the left to be 7 characters wide.

Is there a way to do the same, as efficiently, in GLB?
Title: Re: Question about string handling.
Post by: Kitty Hello on 2010-Sep-30
something with FORMAT$, maybe?
Title: Re: Question about string handling.
Post by: Ian Price on 2010-Sep-30
I've done this with a few of my GLB games, eg

(http://www.iprice.remakes.org/my_stuff/bli2x.PNG)

but there's no method as efficient as your BlitzMax example.

I use LEN to find length of the score and used a FOR/NEXT loop to add the leading zeros - I use something like this, but with a proper bitmap font routine.

Code (glbasic) Select

GLOBAL sc=1

WHILE TRUE

FOR n=0 TO 8-LEN(sc)
PRINT "0",n*10,10
NEXT

PRINT sc,(9-LEN(sc))*10,10

INC sc

SHOWSCREEN

WEND
Title: Re: Question about string handling.
Post by: ampos on 2010-Sep-30
while len(a$)<8:a$="0"+a$:wend
Title: Re: Question about string handling.
Post by: MrTAToad on 2010-Sep-30
Most efficient way is :

PRINT RIGHT$("000000"+score%,len(6)),0,0

Been using that since the C64 :)
Title: Re: Question about string handling.
Post by: ampos on 2010-Sep-30
Quote from: MrTAToad on 2010-Sep-30
Most efficient way is :

PRINT RIGHT$("000000"+score%,len(6)),0,0

Been using that since the C64 :)

Lol, you are winning the competition...
Title: Re: Question about string handling.
Post by: Ian Price on 2010-Sep-30
RIGHT$ didn't exist in GLB until recently, so I got used to using a different method.

At least mine actually works MrTaToad (test yours) ;)
Title: Re: Question about string handling.
Post by: hardyx on 2010-Sep-30
I think this sentence is the equivalent in GLBasic:

PRINT "score: " + REPLACE$( FORMAT$(7, 0, Score), " ", "0"), 0, 0
Title: Re: Question about string handling.
Post by: Ian Price on 2010-Sep-30
Quote
PRINT "score: " + REPLACE$( FORMAT$(7, 0, Score), " ", "0"), 0, 0

The winner so far :)
Title: Re: Question about string handling.
Post by: Kitty Hello on 2010-Sep-30
I think the right$ thing might evaluate a tad faster, though.
Title: Re: Question about string handling.
Post by: Sokurah on 2010-Sep-30
Quote from: MrTAToad on 2010-Sep-30
Most efficient way is :

PRINT RIGHT$("000000"+score%,len(6)),0,0

Been using that since the C64 :)

Haha, I thought there wouldn't be an answer so I just made this, then came here to post it and saw there was 100 replies.  =D

PRINT (LEFT$("000000",6-LEN(Score%))+Score%),0,0

Mine, compared to yours, also has the bonus of actually working. With a score of 8192 your example just prints 2.

Edit: Great. I ended up solving it my own way, but thanks for the help everybody. And MrTAToad, I'll try not to say something funny about how things related to the C64 don't work. Oops.  =D  :whistle:
Title: Re: Question about string handling.
Post by: Moru on 2010-Sep-30
Does it have to be so many complicated solutions?  ;)

Code (glbasic) Select
RIGHT$("000000"+score, 6)
Title: Re: Question about string handling.
Post by: Sokurah on 2010-Sep-30
Quote from: Moru on 2010-Sep-30
Does it have to be so many complicated solutions?  ;)

Code (glbasic) Select
RIGHT$("000000"+score, 6)

LOL, even shorter. I don't think it gets any better than this. ;)
Title: Re: Question about string handling.
Post by: MrTAToad on 2010-Sep-30
QuoteAt least mine actually works MrTaToad (test yours)
Yes, Mr Smarty-pants - there was a slight mistake in the code  :D

It should really be something like :

Code (glbasic) Select
SCORE_PAD$="000000"
PRINT RIGHT$(SCORE_PAD$+score%,LEN(SCORE_PAD$)),0,0


Actually, I used to use RIGHT$ as a function which just used MID$ (as does the official command)
Title: Re: Question about string handling.
Post by: MrTAToad on 2010-Sep-30
Quote from: Moru on 2010-Sep-30
Does it have to be so many complicated solutions?  ;)

Code (glbasic) Select
RIGHT$("000000"+score, 6)
But what if you add or subtract a 0 and forget to update the number 6 ? :)
Title: Re: Question about string handling.
Post by: matchy on 2010-Oct-01
Quote from: Kitty Hello on 2010-Sep-30
I think the right$ thing might evaluate a tad faster, though.

How about the speed of MID$ command?
Title: Re: Question about string handling.
Post by: Kitty Hello on 2010-Oct-01
left$, right$, mid$ are basically the same command. The replace might be a tad slower when the string lengths of needle$ and haystack$ are not equal.
Title: Re: Question about string handling.
Post by: ampos on 2010-Oct-03
The problem with most of your function is/was that numbers converted to strings used to have a space in front (the hidden + (positive) sign) so you used to do:

A$=right$(str$(n),Len(str$(n))-1) just to remove this blank, on the c64 and others.
Title: Re: Question about string handling.
Post by: MrTAToad on 2010-Oct-03
Well, the C64 didn't do automatic type conversion in BASIC :)