Jump to content
amir0914

Length in a List

Recommended Posts

amir0914

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?

 

Share this post


Link to post
Share on other sites
amir0914

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

 

Share this post


Link to post
Share on other sites
Tharwat

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")))

 

  • Like 1

Share this post


Link to post
Share on other sites
amir0914

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"

Share this post


Link to post
Share on other sites
Tharwat

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"))

 

  • Like 1

Share this post


Link to post
Share on other sites
amir0914

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.

Share this post


Link to post
Share on other sites
dlanorh

Try

 

(vl-position "Railway_Station" '("Bridge" "Building" "Canal" "Gas_Pipe" "Railway_Station"))

 

  • Like 1

Share this post


Link to post
Share on other sites
Tharwat
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))
  )

 

  • Like 1

Share this post


Link to post
Share on other sites
ronjonp
Posted (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 by ronjonp
  • Like 1

Share this post


Link to post
Share on other sites
amir0914

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. 

 

Share this post


Link to post
Share on other sites
ronjonp
15 minutes ago, amir0914 said:

Hi ronjonp, it's exactly what i want.Thank you very much,

Glad to help :)

  • Like 1

Share this post


Link to post
Share on other sites
Lee Mac

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"

 

  • Like 2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×