Shop OBEX P1 Docs P2 Docs Learn Events
Problem with Arrays of Longs — Parallax Forums

Problem with Arrays of Longs

edited 2011-10-09 13:19 in Propeller 1
Hello Everyone, :smile: (This is my first post)


I have a programming issue I was wandering if you could help me with.
I have connected a keyboard and a parallel LCD to my propeller. I have repeat loop running checking for key presses. Once they are found each character returned from .getkey is stored in an array of long numbers. The code looks more or less like this:
…
VAR
  long CommandLine [20]     'The array
…
index := 0 
repeat                                                   
  if KB.gotKey                                                   
    CommandLine[index] := KB.getKey                          
    LCD.pos(index+1,0)                                          
    LCD.out(CommandLine[index]                         
…
    Index := index + 1                                           
…
  
The LCD displays these characters properly. The problem arises with the following function:
 
PUB Command (COM) | i
  result := true
  i := 0 
  repeat i from 0 to StrSize(COM) - 1
    if (CommandLine[i] <>= COM[i])  
      result := false
 
I give it a string eg. Command(“TEST”) and I want it to test if the leters T – E – S – T match those stored into the first positions of the array CommandLine[0] CommandLine[1] CommandLine[2] CommandLine[3] respectively. If all characters match I want it to return true and if not false. The code seems correct, but the if statement somehow doesn’t work!



Any help would be of great value, and I would like to thank you all in advance.

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2011-10-08 03:41
     
    PUB Command (COM) | i
      result := true
      i := 0 
      repeat i from 0 to StrSize(COM) - 1
        if (CommandLine[i] <>= COM[i])  
          result := false
     
    
    It's close enough. I assume you meant Command(string("TEST")). In this case COM is the address of the string and can be dereferenced with byte[COM]. Also, why do you want to use <>= instead of <>? The former is an assignment operator and will overwrite your command line array.
    PUB command(COM) | i
    
      repeat i from 0 to StrSize(COM) - 1
        if (CommandLine[i] <> byte[COM][i])
          return FALSE                         ' report errors as early as possible
    
      return TRUE
    
    In order to save memory you should declare CommandLine as byte array not long.
  • edited 2011-10-08 08:42
    Thanks kuroneko it all worked out! Turns out black cats bring good code, who knew? I really learned a lot from this post and wanted to thank you.
  • Mike GreenMike Green Posts: 23,101
    edited 2011-10-08 08:53
    Please change your thread from [ unsolved ] to [ solved ]. You can go to your 1st message and click Edit Post to change this.
  • edited 2011-10-09 08:08
    All Done! And thanx once again to everyone. :)
  • Cluso99Cluso99 Posts: 18,069
    edited 2011-10-09 13:19
    manolispotetsianakis: Glad you problem is solved.
    But, we missed welcoming you, so welcome to this fabulous prop forum. As you can see, we are a friendly lot and it doesn't take long to get replies of assistance. SO don't hesitate to ask questions or comment.
Sign In or Register to comment.