Determening the nth item in a list

Discussion in 'AutoCAD' started by Marcel Janmaat, Dec 16, 2004.

  1. Lets say I have a list like this

    '(a b c d e f g h i j)

    How can I determin the nth of h in this list?

    M
     
    Marcel Janmaat, Dec 16, 2004
    #1
  2. Marcel Janmaat

    Tom Smith Guest

    (defun nthof (sym lst / mlst)
    (if (setq mlst (member sym lst))
    (-
    (length lst)
    (length mlst))))
     
    Tom Smith, Dec 16, 2004
    #2
  3. Marcel Janmaat

    BillZ Guest

    Marcel,

    Command: (setq lst1 '(a b c d e f g h i j))
    (A B C D E F G H I J)

    Command: (setq lst2 (member 'h '(a b c d e f g h i j)))
    (H I J)

    Command: (setq indx (- (length lst1)(length lst2)))
    7

    Command: (nth indx lst1)
    H


    Bill
     
    BillZ, Dec 16, 2004
    #3
  4. Thanx!

    That was my idea too.
    Just thought there might be a direct command for it.

    M
     
    Marcel Janmaat, Dec 16, 2004
    #4
  5. Marcel Janmaat

    Tom Smith Guest

    Great minds ... :)
     
    Tom Smith, Dec 16, 2004
    #5
  6. (vl-position)

    --
    R. Robert Bell


    Lets say I have a list like this

    '(a b c d e f g h i j)

    How can I determin the nth of h in this list?

    M
     
    R. Robert Bell, Dec 16, 2004
    #6
  7. HA!
    I knew it!
    Not such a stupid question after all.

    The less code the better. Thats my motto.

    What do you say to that mister "great mind" Tom Smith? ; )

    Thanx Robert.
     
    Marcel Janmaat, Dec 16, 2004
    #7
  8. Marcel Janmaat

    Tom Smith Guest

    What do you say to that mister "great mind" Tom Smith? ; )

    I hoped you'd look back for that Marcel! I'm not fluent with all the
    vl-do-this-and-that functions and gave them only a quick glance before
    writing a function to do this. Leave it to Robert to know these things.

    My joke to Bill was a reference was to the old expression "great minds think
    alike" because we had come up with identical code independently -- it is
    always used in a joking way, for instance when people happen to say the same
    thing at the same time.
     
    Tom Smith, Dec 16, 2004
    #8
  9. Marcel Janmaat

    BillZ Guest

    Marcel,

    We're all learning something new everyday.
    Most of what I've learned has been from someone showing me a better way.
    That's what keeps me participating.

    (vl-position 'h '(a b c d e f g h i j))

    Thanks Robert.

    Bill
     
    BillZ, Dec 16, 2004
    #9
  10. Marcel Janmaat

    Tom Smith Guest

    why the ' ????? (vl-position 'h '(a b c d e f g h i j))

    Luis, in both the vl-position function and my home-brewed equivalent, if you
    don't quote the symbol, it will be evaluated. If the symbol h hasn't been
    assigned a value, it evaluates to nil.

    _$ (vl-position h '(a b c d e f g h i j))
    nil
    _$ (eval h)
    nil
    _$ (vl-position nil '(a b c d e f g h i j))
    nil

    _$ (setq newvar 'h)
    H
    _$ (vl-position newvar '(a b c d e f g h i j))
    7
    _$ (vl-position 'h '(a b c d e f g h i j))
    7
     
    Tom Smith, Dec 16, 2004
    #10
  11. I was just kidding Tom. All input is appreciated.
    You 'd be happy to know, at first I used yours. : )
    But now I use (vl-position).

    M
     
    Marcel Janmaat, Dec 16, 2004
    #11
  12. I know Bill. Same for me.

    FWIW

    I placed a sample of the command(s) you've helped me with on the
    autodesk.autocad.customer-files section.
    I use it to place a variaty of symbols in- or next to a line, a groups of
    symbols divided over a rectangular area or just replace a symbol.
    I had written it some time ago but enhanced it with options for extra
    paramters and the selection borders on the image_button.

    Just thought, maby you 'd like to see what it looks like.

    M
     
    Marcel Janmaat, Dec 16, 2004
    #12
  13. Unfortunatly I cannot seem to post it.

     
    Marcel Janmaat, Dec 16, 2004
    #13
  14. It is sort of a bogus list, Luis. It isn't all that often that you have a
    list of symbols, but rather a list of data.

    (vl-position "h" (list "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"))
     
    R. Robert Bell, Dec 17, 2004
    #14
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.