Array Question:


Closed Thread
Results 1 to 12 of 12

Thread: Array Question:

Hybrid View

  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.

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