Amiga.org
Operating System Specific Discussions => Amiga OS => Amiga OS -- Development => Topic started by: melott on February 18, 2003, 02:08:40 PM
-
So....
For x=0 to 255
Print chr$(x) ;
Gosub LineLen
Next x
end
LineLen:
for y=1 to 25
If y<25 then Return
If y=25 then Print chr$(13)
next y
What I'm tring to do is print 25 characters
per line then a carrage return and start another
line until X reaches 255.
This doesn't work .
Anyone????
And how do I do a carrage return without
doing a Print chr$(13) ?
Mel Ott
:-?
-
I'm no BASIC man, as anybody checking the developer forum could vouch for by now ;-), but I'll have a go..
I think your problem comes down to the fact that your 'y' variable is initialized with the value 1 every time you call the subroutine. The subroutine returns immediately because of the 'if y<25 then return' condition. Consequently, y never gets to 25 and you don't get your carriage return.
You could just use a modulus of your 'x' value in the main loop. Not sure about the syntax, but something like
for x=0 to 255
print chr$(x)
if x%25 == 0 then print chrs$(13)
next x
Does BASIC have a modulus anybody?
In any event, bear in mind that there are lots of unprintable characters before when x < 32
-
Oh dear...
Here is how it could work:
len = 0
for i=32 to 255
print chr$(i);
len = len + 1
if len >= 25 then
print
len = 0
endif
next
Note: characters below 32 are not printable
Example 2:
a$ = ""
for i=32 to 255
a$ = a$ + chr$(i)
if len(a$) >= 25 then
print a$
a$ = ""
endif
next
Example 3:
for i = 32 to 231 step 25
for j=0 to 24
print chr$(i+j);
next
print
next
for j=232 to 255
print chr$(j);
next
print
Note: this one would work better if the line length (25) was a divider of 256 (e.g. 32).
Bye,
Thomas
-
The Amiga does not use Chr$(13) for line
termination. It uses Chr$(10) which
produces a and . A will just
restart printing at the beginning of the
same line.
-
well, as someone already stated, that LineLen routine just doesn't do anything useful.
But I think this can be accomplished in one line, using modulo division, just divide y by 25, and if the "remainder" (from hand calculated long division)
is zero then you have a multiple of 25, and its time to go to the next line.
I do not know how to write that in Amiga basic, but here is some pseudo code:
after printing x, then just check:
if x%25=0 then print;
EDITED......
oh crud, just saw someone already gave the answer, well, let me say I agree!!!
;-) ;-) ;-)
-
Does BASIC have a modulus anybody?
I think it's mod , but I'm not sure and can't remember how to use it if it is. However,
let n=25
...
if (x/n) = int(x/n) then
blah blah blah
will do the same in AmigaBasic MOST of the time. Certainly for small numbers AmigaBasic was pretty good about rounding its floats. I'd stay away from it though because, while it may be mathematically correct, computers aren't.
Safer way, if you don't have mod is to just count:
if count > 0 then
{
print char(x)
count--
}
else
{
print
count = n
}
-
Oh well, just checked an online faq and there doesn't appear to be a mod operator.
Thomas' number 2 looks nice. But for speed, counting beats LEN$ ing. Then again, if you want speed go C.
-
If you want speed, just unravel the loops....
I mean
print chr$(33)
print chr$(34)
print chr$(35)
etc...etc....
print chr$(50)
print chr$(13)
print chr$(51)
this is a legitimate practice, unraveling loops!
<--everyone stares at me in horror -->
also you could probably have a 'DATA' field doing the same thing, which would look a little cleaner
psuedo code:
for x = 33 to 264
print read data
next x
DATA 33,34,35, etc.etc.etc., 50,13,51
sure C is faster, so is machine code...and btw, this would be faster if he had all the codes memorized and didn't need to read them. :-D :-D :-D
-
That LineLen thing is a waste of code. :-) Someone pointed out that it will always return without counting because y always starts out <25. I didn't catch that. :-(
What I thought was that it's a waste of time to count to 25 if the loop does nothing but count to 25. Which, is what it will do if you remove the return if y<25. Why not just take out the y loop and just put in a y=y+1 and a if y=25 then y=0 and print this character. The loop does nothing for you.
What results are you getting with the way the code is now? I'm assuming you're either getting a 256 long listing 1 character wide or a 1 long listing 256 characters wide(if the carriage return doesn't work).
-
Thanks guys....
I guess I just needed a little push in a different
direction. This works...........
Y=0
FOR X=0 to 255
PRINT CHR$(X) ;
Y=Y+1
IF Y=25 THEN PTINT CHR$(10) : Y=0
NEXT X
Mel Ott
:-D
-
For x=0 to 255
Print chr$(x) ;
Gosub LineLen
Next x
end
LineLen:
for y=1 to 25
If y<25 then Return
If y=25 then Print chr$(13)
next y
Yikes! This code isn't doing things you want it to do!
On the first iteration, you are entering the "For X" loop. Then, you are doing a GOSUB, and entering a "For Y" loop. This is all valid. You have a nested "For...Next loop". What goes wrong is that you are then issuing a RETURN statement before your "Next Y" statement. Essentially, if you remove the GOSUB here, you'll see the problem.
For x=0 to 255
Print chr$(x) ;
for y=1 to 25
If y<25 then Next x
If y=25 then Print chr$(13)
next y
end
It's not valid to nest loops in the style of
For X
For Y
Next X
Next Y
The reason being that you will initialize multiple instances of the For Y loop, without finishing them. In your case, 255 For Y loops will be started. This will blow the stack on your BASIC interpreter and either return an error, or cause a memory leak. Even if by some miracle your BASIC interpreter has enough stack to get by this problem, you'll run into a "Next without For" condition on the Next X once the For X loop has run past 255.
The other methods previous posters have typed are much cleaner. :-)
-
melott wrote:
IF Y=25 THEN PTINT CHR$(10) : Y=0
Just use "print"
Chr$(10) was meant for printers to drop down a line.
Also Chr$(28 29 30 and 31) one is move cursur up, one is down, one is left, one is right, without erasing the letter that is there.
Amiga!
-
Thanks ...
I posted, up a reply or two, that I got it
worked out. Yes the "LINELEN" thing wasn't
the way to go. I'm a beginner and it takes
time to learn.
Yes I know that the first 32 or so characters
don't print. It was only a test example to
work out the code. What I was looking for
is to get a specific number of chrs. reguardless
what they are to print on a line then goto
the next line.
Mel Ott