Jump to content

Recommended Posts

Posted

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?

 

Posted

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

 

Posted

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
Posted

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"

Posted

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
Posted

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.

Posted

Try

 

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

 

  • Like 1
Posted
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
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
Posted

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. 

 

Posted
15 minutes ago, amir0914 said:

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

Glad to help :)

  • Like 1
Posted

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

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...