Shop OBEX P1 Docs P2 Docs Learn Events
lookup and lookdown — Parallax Forums

lookup and lookdown

Dave HeinDave Hein Posts: 6,347
edited 2010-02-12 03:23 in Propeller 1
lookup and lookdown have the features described in the propeller manual, but they also have an undocumented·conditional-execution feature.· lookdown will evaluate each expression until it finds a matching value.· lookup will evaluate the first N expressions in the list, where N is the index value.· Here are a couple of examples:

x := 6
lookdown(5 : x--, x--, x--)

I believe this will return an index of 2, and x will have a final value of 4.· Only the first two "x--" expressions are executed, but not the third.· lookup seems to execute expression sequentially up to the selected item, just like lookdown.· Here is an example of lookup.

x := 6
lookup(3 : x++, x++, x++, x++, x++)

This should return a value of 8, and the value for x will be 9.· Does anybody use the conditional evaluation feature of lookup and lookdown?· Should it be documented in the propeller manual, or is it best not to depend on this feature in the future?· One use I can see is to emulate the C language conditional evaluation feature, such as in the following expression:

if (x > 5 || x-- >· 0)

which could be written in Spin as

if lookdown(true : x > 5, x-- > 0)

Dave

Comments

  • Dave HeinDave Hein Posts: 6,347
    edited 2010-02-11 16:40
    It appears that nobody is very interested in lookup or lookdown.· In most applications lookup can be implemented with an array of numbers.· If expressions need to be evaluated, a case statement could be used instead of lookup.

    lookdown is a little more interesting, and it could be implemented with the case statement as well.· Does anybody use lookdown, or do you prefer to use case to perform that function?

    Dave
  • KyeKye Posts: 2,200
    edited 2010-02-11 16:49
    Dave, while that is interesting feature. I think it may be going off the deepend in code optimization.

    The potential for bugs there is rather large...

    Thanks for the info there however. I did not know about this.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
  • Cluso99Cluso99 Posts: 18,069
    edited 2010-02-11 18:56
    Dave: I have never used lookup or lookdown (nor bothered to understand it either).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Links to other interesting threads:

    · Home of the MultiBladeProps: TriBlade,·RamBlade,·SixBlade, website
    · Single Board Computer:·3 Propeller ICs·and a·TriBladeProp board (ZiCog Z80 Emulator)
    · Prop Tools under Development or Completed (Index)
    · Emulators: CPUs Z80 etc; Micros Altair etc;· Terminals·VT100 etc; (Index) ZiCog (Z80) , MoCog (6809)·
    · Prop OS: SphinxOS·, PropDos , PropCmd··· Search the Propeller forums·(uses advanced Google search)
    My cruising website is: ·www.bluemagic.biz·· MultiBlade Props: www.cluso.bluemagic.biz
  • RaymanRayman Posts: 14,877
    edited 2010-02-11 19:00
    I think that's very interesting! Could be useful in certain cases...

    I just used lookup for my virtual keyboard to translate x tile location into a keycode...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    My Prop Info&Apps: ·http://www.rayslogic.com/propeller/propeller.htm
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2010-02-11 19:01
    I just used my first one ever only two days ago. Just to find a better way to do it a few minutes later.
    Though it is an interesting feature, it does seem like there would be better, more readable/understandable ways to do it.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    April, 2008: when I discovered the answers to all my micro-computational-botherations!

    Some of my objects:
    MCP3X0X ADC Driver - Programmable Schmitt inputs, frequency reading, and more!
    Simple Propeller-based Database - Making life easier and more readable for all your EEPROM storage needs.
    String Manipulation Library - Don't allow strings to be the bane of the Propeller, bend them to your will!
    Fast Inter-Propeller Comm - Fast communication between two propellers (1.37MB/s @100MHz)!
  • JonnyMacJonnyMac Posts: 9,208
    edited 2010-02-11 21:28
    I just replaced a case structure with lookupz -- I'm working on a command processor for new product. The idea is to advance cmdstate if the header characters are received in the correct order.

    repeat
      c := com1.rxcheck                                           ' check serial input
      if (c => 0)                                                 ' if char available
        if (ucase(c) <> lookupz(cmdstate++: "!EFX"))              ' validate header
          cmdstate := 0
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon McPhalen
    Hollywood, CA
  • Dave HeinDave Hein Posts: 6,347
    edited 2010-02-11 23:05
    JonnyMac said...
    I just replaced a case structure with lookupz -- I'm working on a command processor for new product. The idea is to advance cmdstate if the header characters are received in the correct order.

    repeat
      c := com1.rxcheck                                           ' check serial input
      if (c => 0)                                                 ' if char available
        if (ucase(c) <> lookupz(cmdstate++: "!EFX"))              ' validate header
          cmdstate := 0
    

    That's an interesting use of lookup.· Wouldn't it also work with the following code?
    DAT
    · header byte "!EFX"
    ...
    · repeat
    ··· c := com1.rxcheck·········································· ' check serial input
    ··· if (c => 0)···················································· ·' if char available
    ····· if (ucase(c) <> byte[noparse][[/noparse]header][noparse][[/noparse]cmdstate++])······· ' validate header
    ······· cmdstate := 0
  • JonnyMacJonnyMac Posts: 9,208
    edited 2010-02-11 23:33
    Yep, I bet that would work -- but I think the lookupz version is a tad more readable (and for my customers that's REALLY important). That said, I always reserve the right to be wrong and may switch to your version!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon McPhalen
    Hollywood, CA
  • Dave HeinDave Hein Posts: 6,347
    edited 2010-02-12 03:23
    The reason I asked my initial question about lookup and lookdown is that I'm writing a program to convert Spin to assembly.· I'm trying to determine whether I should duplicate the conditional execution aspect.· Since this is not documented, I don't think I'll duplicate the same method for lookup.· I'll just evaluate the indexed expression, and not all the previous ones.

    I looked at the Spin interpreter code, and it uses the same section of code to implement lookup, lookdown and the case statement.· That's why lookup evaluates the first N expressions.

    Dave
Sign In or Register to comment.