PDA

View Full Version : Array Question:



Archangel
- 28th May 2008, 00:46
Hello,
I am trying to get a better understanding of arrays, and have written the code below. I "think"
it should output the letters "A" through "j" ;however, it outputs A - K. Anyone know why?


@ __config _HS_OSC & _WDT_OFF & _PWRTE_ON & _LVP_OFF & _BODEN_OFF
define OSC 20
include "modedefs.bas"
LETTERS VAR BYTE
String var byte[10]
serout PortB.6,T9600,[254,128,"Loop Check"]
pause 500
serout PortB.6,T9600,[$FE,1]
string[0]= "A"
string[1]= "B"
string[2]= "C"
string[3]= "D"
STRING[4]= "E"
STRING[5]= "F"
STRING[6]= "G"
STRING[7]= "H"
STRING[8]= "I"
STRING[9]= "J"

loop:
FOR letters = STRING[0] to STRING[9]

serout PortB.6,T9600,[254,128,"string is ",(LETTERS)]
pause 500
NEXT letters
goto loop

Jerson
- 28th May 2008, 04:30
Your code
loop:
FOR letters = STRING[0] to STRING[9]

serout PortB.6,T9600,[254,128,"string is ",(LETTERS)]
pause 500
NEXT letters
goto loop


I think it should be done like this


for letters = 0 to 9
serout PortB.6,T9600,[254,128,"string is ",(string[LETTERS])]
next

Archangel
- 28th May 2008, 05:09
Hi Jerson, That fixed it all right, still puzzels me as to why the 10 byte array held 11 though, the way I wrote it. . . Thank You, I will ponder this some more . . .
JS

Bruce
- 28th May 2008, 16:20
Hi Joe,

What version of PBP are you using?

Your first version should work just fine. FOR letters = STRING[0] to STRING[9] is essentially
the same as FOR letters = "A" to "J", which works the same.

This;


DEFINE OSC 20
INCLUDE "modedefs.bas"
LETTERS VAR BYTE
String VAR BYTE[10]
string[0]= "A"
string[1]= "B"
string[2]= "C"
string[3]= "D"
STRING[4]= "E"
STRING[5]= "F"
STRING[6]= "G"
STRING[7]= "H"
STRING[8]= "I"
STRING[9]= "J"

loop:
FOR letters = STRING[0] to STRING[9]
SEROUT PORTC.6,T9600,["string is ",(LETTERS),13,10]
PAUSE 500
NEXT letters
GOTO loop

END
Outputs this;


string is A
string is B
string is C
string is D
string is E
string is F
string is G
string is H
string is I
string is J
string is A
string is B
string is C
string is D, etc, etc,

mackrackit
- 28th May 2008, 17:16
But where was the "K" coming from???

I do not get a "K".

mister_e
- 28th May 2008, 20:17
loop:
FOR letters = STRING[0] to STRING[9]
SEROUT PORTC.6,T9600,["string is ",(LETTERS),13,10]
PAUSE 500
NEXT letters
GOTO loop

I found it's a weird way... and a bit obscure...i would used


loop:
FOR letters = 0 to 9
SEROUT PORTC.6,T9600,["string is ",STRING[LETTERS],13,10]
PAUSE 500
NEXT letters
GOTO loop

seems more valuable no?

Bruce
- 28th May 2008, 20:52
Well that and the way Jerson pointed out, would for sure be the proper way to go,
but it should still work the way he had it originally as long as characters between string[0]
and string[9] were in order.

If you had values assigned to individual elements like say;

string[0]= "A"
string[1]= "Z"
string[2]= "P"
string[3]= "O"
string[4]= "M"
string[5]= "H"
string[6]= "X"
string[7]= "G"
string[8]= "W"
string[9]= "J"

Then doing it his original way, it wouldn't return the correct values in each element.
It would still print A,B,C,D,E,F,G,H,I,J.

Like Dave, I'm kinda curious where that "K" came from?

mister_e
- 28th May 2008, 20:55
That was my point... hidden between the lines ;) Unless a simple FOR To Next without array would have worked.

Edit: seems i missed Jerson's post :o

mackrackit
- 28th May 2008, 21:33
That was my point... hidden between the lines ;) Unless a simple FOR To Next without array would have worked.

Hidden between what lines? I still do not get it.

Archangel
- 28th May 2008, 21:35
Hi Bruce, Mister_e,Mackracket, . . .
Yeaaaa . . . Kinda weirded me out too, I was using a 16F628A, and yes I forgot to put CMCON=7 in, but I was not using PortA. It very definately went from A - K,though I expected it to stop with "J". Jerson straightened me out, still I do not know why it went to K. PBP ver 2.5

mackrackit
- 28th May 2008, 21:43
Aliens!?!?

mister_e
- 28th May 2008, 21:47
Hidden between what lines? I still do not get it.
i don't know.. is "read between the line" a common and known expression worldwide?

FYI, It was between... "seems" and "more valuable" ;)

well not between any line at all... forget it :D

Time for a beer or twelve i guess. now that i can...