item of a list

Discussion in 'AutoCAD' started by martin, Feb 4, 2004.

  1. martin

    martin Guest

    how can i get an item at a certain index in a list (5th for example) i
    usually use foreach to get the items one by one....
    martin
     
    martin, Feb 4, 2004
    #1
  2. martin

    Fred Wilson Guest

    Check out the "nth" function:

    e.g.:
    (setq FOO (list 0 1 2 3 4 5 6)); FOO euqals (0 1 2 3 4 5 6)
    (nth 5 FOO)

    returns 5 ;nth starts counting from 0
     
    Fred Wilson, Feb 4, 2004
    #2
  3. (nth <index> <list>)
     
    Tony Tanzillo, Feb 4, 2004
    #3
  4. martin

    Rudy Tovar Guest

    I wonder if that was a trick question?



     
    Rudy Tovar, Feb 4, 2004
    #4
  5. martin

    John Uhden Guest

    Or, if you want to know whether/where an item is in a list, then (vl-position
    <item> <list>). nil means it ain't there 'tall.
     
    John Uhden, Feb 5, 2004
    #5
  6. martin

    Adesu Guest

    _$ (setq liststring (list "A" "B" "C" "D" "E"))
    ("A" "B" "C" "D" "E")
    _$ (setq nlist (nth 3 liststring))
    "D"
    _$ (vl-position "D" liststring)
    3
    # Conclusion >> (vl-position symbol list) >< (nth n lst)
     
    Adesu, Sep 24, 2004
    #6
  7. martin

    John Uhden Guest

    True. The (nth) function will return whatever value is in the specified index
    position in a list, or nil if the list is shorter than the index position
    supplied. The (vl-position) function returns the first position (as an integer)
    of a specific item in a list or nil if not found. These functions are not
    identical, but rather complimentary.

    John Uhden, Cadlantic
    <the e-mail address is bogus>
    http://www.cadlantic.com
    Sea Girt, NJ
     
    John Uhden, Sep 27, 2004
    #7
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.