Array Question:


Closed Thread
Results 1 to 12 of 12

Thread: Array Question:

  1. #1
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818

    Default Array Question:

    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?
    Code:
    @ __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
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  2. #2
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    966


    Did you find this post helpful? Yes | No

    Default

    Code:
     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
    Code:
    for letters = 0 to 9
      serout PortB.6,T9600,[254,128,"string is ",(string[LETTERS])]
    next

  3. #3
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default YEP, That fixed her . . .

    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
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  4. #4
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    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;
    Code:
    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;
    Code:
    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,
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  5. #5
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    But where was the "K" coming from???

    I do not get a "K".
    Dave
    Always wear safety glasses while programming.

  6. #6
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Code:
    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
    Code:
    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?
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  7. #7
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    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?
    Last edited by Bruce; - 28th May 2008 at 20:54. Reason: The "K"
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  8. #8
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    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
    Last edited by mister_e; - 28th May 2008 at 20:57.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  9. #9
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mister_e View Post
    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.
    Dave
    Always wear safety glasses while programming.

  10. #10
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    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
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  11. #11
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Aliens!?!?
    Dave
    Always wear safety glasses while programming.

  12. #12
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mackrackit View Post
    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

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

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. Bits, Bytes Words and Arrays
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 24
    Last Post: - 14th June 2016, 07:55
  2. Simple Array Demo
    By Archangel in forum Code Examples
    Replies: 5
    Last Post: - 15th February 2010, 04:46
  3. Array Question....please help
    By jmoskalski in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 31st October 2009, 01:29
  4. How to saparate variable to array and GLCD Question
    By pramarn in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 4th October 2006, 03:42
  5. Stupid array question
    By RUBiksCUbe in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 4th September 2006, 16:03

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts