RepCad Posted January 7, 2019 Posted January 7, 2019 Hi all, I have a problem in a part of my program, I don't know how find Maximum Length in List, Example of List : MyList = ("Excel" "Access" "IDM" "Autocad") in this list length for each element is = ( 5 6 3 7 ) so Maximum Length in my list = 7 , Then i need a code or function to get "7" Can anyone help me? Quote
RepCad Posted January 7, 2019 Author Posted January 7, 2019 Tharwat,Thank you for reply, but it's not what I want, if you read my written carefully , I need to get Max Length of all Text Length. For Example : List = ("Bridge" "Building" "Canal" "Gas_Pipe" "Railway_Station") =>>>> Result = 15 Quote
Tharwat Posted January 7, 2019 Posted January 7, 2019 No worries, I thought you just wanted the max number of the second list and not the primary one. (apply 'max (mapcar 'strlen '("Bridge" "Building" "Canal" "Gas_Pipe" "Railway_Station"))) 1 Quote
RepCad Posted January 8, 2019 Author Posted January 8, 2019 Yes, That's it. Thanks a lot Tharwat. In this code also, can we find Text itself? i mean in that example is =>>>> "Railway_Station" Quote
Tharwat Posted January 8, 2019 Posted January 8, 2019 You're welcome. You can use member function to find a member from a list and be mindful is that the string would be case sensitive in this case. (member "Railway_Station" '("Bridge" "Building" "Canal" "Gas_Pipe" "Railway_Station")) 1 Quote
RepCad Posted January 8, 2019 Author Posted January 8, 2019 Yes I have to use Member function , but I mean is how to find member of maximum length in list. in above example is "Railway_Station" that have maximum lenght. Quote
dlanorh Posted January 8, 2019 Posted January 8, 2019 Try (vl-position "Railway_Station" '("Bridge" "Building" "Canal" "Gas_Pipe" "Railway_Station")) 1 Quote
Tharwat Posted January 8, 2019 Posted January 8, 2019 1 hour ago, amir0914 said: Yes I have to use Member function , but I mean is how to find member of maximum length in list. in above example is "Railway_Station" that have maximum lenght. Once the string is matched in a list then the length of that string would be the one with the desired length. eg: (if (setq f (member "Railway_Station" '("Bridge" "Building" "Canal" "Gas_Pipe" "Railway_Station"))) (strlen (car f)) ) 1 Quote
ronjonp Posted January 8, 2019 Posted January 8, 2019 (edited) Perhaps this is what you want? (setq l '("Bridge" "Building" "Canal" "Gas_Pipe" "Railway_Station")) (car (vl-sort l '(lambda (a b) (> (strlen a) (strlen b))))) Edited January 8, 2019 by ronjonp 1 Quote
RepCad Posted January 8, 2019 Author Posted January 8, 2019 Hi ronjonp, it's exactly what i want.Thank you very much, Also Thanks to Tharwat for sharing that usefull information and appreciate the time you spend for me. Quote
ronjonp Posted January 8, 2019 Posted January 8, 2019 15 minutes ago, amir0914 said: Hi ronjonp, it's exactly what i want.Thank you very much, Glad to help 1 Quote
Lee Mac Posted January 8, 2019 Posted January 8, 2019 FWIW, for longer list lengths, you'll want to consider using a function such as this to determine the extrema for a given function applied over a list, since this will involve far fewer comparisons than a sort operation. For your example, this would be: _$ (setq l '("Bridge" "Building" "Canal" "Gas_Pipe" "Railway_Station")) ("Bridge" "Building" "Canal" "Gas_Pipe" "Railway_Station") _$ (extremum '(lambda ( a b ) (> (strlen a) (strlen b))) l) "Railway_Station" 2 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.