Jump to content

Length in a List


RepCad

Recommended Posts

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?

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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"

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...