Shop OBEX P1 Docs P2 Docs Learn Events
Having problem getting bytes from DAT with passed in address — Parallax Forums

Having problem getting bytes from DAT with passed in address

fishfish Posts: 55
edited 2007-06-21 20:29 in Propeller 1
Hi,
I am trying to do somthing·really simple, but I'm stuck.
Method testMe() looks up an address at index·0 and·passes that address to showNumber()·who·then·displays the data.
But it's not working out·the way I expected.·
Can somebody please clue me in on what·am I doing·wrong?
DAT
   _A byte 0,9,4,0,4,0,7,9,2,5,6,5,-1
  
pub testMe | index,addr
  index := 0
  addr:= lookup(index : _A) 
  showNumbers(addr)
 
pub showNumbers(addr) | val, i 
  i:=0
  repeat until val==-1   
      val := byte[noparse][[/noparse]@addr][noparse][[/noparse]i++]   'I get, well.. not what I want.
      ' val := byte[noparse][[/noparse]@_A][noparse][[/noparse]i++]   'uncomment to see the expected results
      term.dec(val)
      term.out(13)


thx
fish

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Did I make a unit test for it? No... Did·I run the test·suite·before checking it in? No... But it shouldn't break anything.· ~intern·(just before we let him go).

Comments

  • CardboardGuruCardboardGuru Posts: 443
    edited 2007-06-21 17:45
    One problem is that LOOKUP doesn't work how you think it does. After the colon there must be a column seperated list of values, not a reference to such a list elsewhere.
  • RichardFRichardF Posts: 168
    edited 2007-06-21 18:01
    fish,
    Instead of addr:= lookup(index : _A), use addr:= _A[noparse][[/noparse]index]
    index is the location of the data you want.
    _A is just an array(index).

    Richard
  • fishfish Posts: 55
    edited 2007-06-21 18:28
    Hi Richard, thanks.
    Not quite what I am getting at though.
    And upon closer inspection and what CardboardGuru is getting at is LOOKUP/Z does not give me what I want.
    So it is not not a reference passing problem at all (as this thread title suggests), but rather a problem of obtaining the address to the array.
    So a more complete picture of what I am trying to do is:

    DAT
    _A 1,2,4,5,6,-1
    _B 6,3,6,7,-1
    _C 1,3,-1
    _D 3,2,1,5,0,-1
    _E 5,2,7,5,1,5,3,5,2,-1
    ...

    So a given index would point to an array in memory.
    index 0 would give me array _A
    index 1 would give me array _B
    index 2 would give me array _C
    and so on.

    I believe I can iterate thru the array just fine ( val := byte[noparse][[/noparse]@correctAddress][noparse][[/noparse]i++] )
    It's getting the correct address to that array based upon a given index that I am having trouble with.

    thx

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ". . . the fog is rising"~ Emily Dickinson
  • CardboardGuruCardboardGuru Posts: 443
    edited 2007-06-21 19:14
    How about this?

    val := byte[noparse][[/noparse]lookupz( array_number: @_A, @_B, @_C, @_D, @_E)][noparse][[/noparse]i++]
  • RichardFRichardF Posts: 168
    edited 2007-06-21 19:51
    fish,
    Sorry, I missunderstood. I like what Cardboard just said but I don't know ....
    Let us know if it works. It would be another good tool.
    Richard
  • fishfish Posts: 55
    edited 2007-06-21 20:29
    Ah! so the only problem with my code (other than using lookup instead of lookupz, duh)

    was the @ in the wrong places.

    This works:

    DAT
       _A byte 0,9,4,0,4,0,7,9,2,5,6,5,-1
      
    pub testMe | index,addr
      index := 0
      'addr:= lookup(index : _A)  ' Was
      addr:= lookupz(index : @_A) ' is now
      showNumbers(addr)
     
    pub showNumbers(addr) | val, i 
      i:=0
      repeat until val==-1   
          'val := byte[noparse][[/noparse]@addr][noparse][[/noparse]i++]  'was  
          val := byte[noparse][[/noparse]addr][noparse][[/noparse]i++]    'is now
          term.dec(val)
          term.out(13)
    

    -btw, I am wondering why have·zero-based·AND·one-based lookup methods?

    From the manual:
    Explanation
    LOOKUP and LOOKUPZ are commands that retrieve entries from a list of values. LOOKUP returns
    the value from ExpressionList that is located in the one-based position (1..N) given by Index.
    LOOKUPZ is just like LOOKUP except it uses a zero-based Index (0..N-1). For both commands, if
    Index is out of range then 0 is returned.
    

    Moreover, why does Spin have·one-based indexing at all?·If it's because·beginning programmers·have trouble·wrapping their minds around·the zeroeth element concept, I will be disappointed. Seems like it would introduce more confusion and 'off by one' errors than it would mitigate... But there must·be·a good reason.

    Anyway, thanks for the help guys!

    fish




    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ". . . the fog is rising"~ Emily Dickinson

    Post Edited (fish) : 6/21/2007 8:37:02 PM GMT
Sign In or Register to comment.