Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

which element of a list?

7 REPLIES 7
Reply
Message 1 of 8
Anonymous
562 Views, 7 Replies

which element of a list?

Hi!

I'm looking for a function, or a lisp routine, that is capable of this:
i have a list, for example (1 2 4 8), and i need to get, that "2" is the 1st element of this,"4" is the 2nd, and so on...
is there any function or stg that can do that?

thx in advance
7 REPLIES 7
Message 2 of 8
Anonymous
in reply to: Anonymous

(setq lst (list 1 2 3 4))
(1 2 3 4)
_$ (setq 1st (car lst))
1
_$ (vl-remove 1st lst)
(2 3 4)
Message 3 of 8
Anonymous
in reply to: Anonymous

Given a list: (1 2 4 8)

There are a few ways.

(setq lst (cdr lst)); returns (2 4 8)

(setq first (nth 0 lst)); returns 1

(setq second (nth 1 lst)); returns 2

(setq third (nth 2 lst)); returns 4

(setq first (car lst)); returns 1

(setq second (cadr lst)); returns 2



Bob
Message 4 of 8
Anonymous
in reply to: Anonymous


There MAY be some confusion about your
problem.

 

If you want to know what value is at a specific
index of your list you could use the nth function

ie;

(setq theList (list 1 2 4 8 16))

face=Arial size=2>

(setq valueAtPosition_3 (nth 3 theList))  ;=> 8 ;
value at index position 3 is 8

 

whereas if you want to know the index location of a specific value you
could use the  vl-position function

 

ie;

(setq LocationOFValue_4 (vl-position 4 theList)) ;=> 2 ; index position
of symbol 4 in list is 2

 

 

///kdub


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
Hi!
I'm looking for a function, or a lisp routine, that is capable of this: i have
a list, for example (1 2 4 8), and i need to get, that "2" is the 1st element
of this,"4" is the 2nd, and so on... is there any function or stg that can do
that? thx in advance
Message 5 of 8
Anonymous
in reply to: Anonymous

Hi!

Thanks a lot for your help 🙂 you are right,the way I put my question was a bit confusing, but I noticed it only after receving some answers I had not really expected.
However you could catch my mind, and your tip seems to be the solution for my problem.

Thanks again:)
Message 6 of 8
Anonymous
in reply to: Anonymous

sunchase wrote:
> Hi! I'm looking for a function, or a lisp routine, that is capable of
> this: i have a list, for example (1 2 4 8), and i need to get, that "2"
> is the 1st element of this,"4" is the 2nd, and so on... is there any
> function or stg that can do that? thx in advance

As others have shown, vl-position is probably what you are after.

Your example is inconsistent, though: "2" is not included in your list,
neither is "4". (The numbers 2 and 4 are, "4" is not a number, it is a
string!)

So, if your data to compare is really strings, you need to have a type
conversion somewhere in your program. See ATOI.

--
Message 7 of 8
Anonymous
in reply to: Anonymous

(setq sample_list (list "first" "second" "third" "fourth"))

then

(length (member "second" (reverse sample_list)))

returns 2,

(length (member "third" (reverse sample_list)))

returns 3, and

(length (member "not_in_list" (reverse sample_list)))

returns 0.
Message 8 of 8
Anonymous
in reply to: Anonymous

Yes, that"s what I needed, you're right 🙂 and I'm sorry for the confusing outlook of my lines: I did not mean to emphasise that 2 or 4 are strings, of course they are numbers. The signs "" must have been embarrassing.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


Autodesk Design & Make Report