Get longest STRLEN of multiple variables

Get longest STRLEN of multiple variables

Anonymous
Not applicable
1,388 Views
7 Replies
Message 1 of 8

Get longest STRLEN of multiple variables

Anonymous
Not applicable

I have multiple STRLEN variables and I'm looking to find the longest of them all.

It doesn't matter which variable is longest, I just need to know the length of the longest string from these variables

 

(SETQ HDRLG(STRLEN HEADER))
(SETQ THRLG(STRLEN THLBL))
(SETQ JTLG1(STRLEN JTLBL1))
(SETQ JTLG2(STRLEN JTLBL2))
(SETQ JTLG3(STRLEN JTLBL3))
(SETQ JTLG4(STRLEN JTLBL4))
(SETQ JTLG5(STRLEN JTLBL5))
(SETQ JTLG6(STRLEN JTLBL6))
(SETQ JTLG7(STRLEN JTLBL7))
(SETQ JTLG8(STRLEN JTLBL8))

 

Thanks in advance,

Larry

0 Likes
Accepted solutions (1)
1,389 Views
7 Replies
Replies (7)
Message 2 of 8

Alexander.Rivilis
Mentor
Mentor
(max
 (SETQ HDRLG(STRLEN HEADER))
 (SETQ THRLG(STRLEN THLBL))
 (SETQ JTLG1(STRLEN JTLBL1))
 (SETQ JTLG2(STRLEN JTLBL2))
 (SETQ JTLG3(STRLEN JTLBL3))
 (SETQ JTLG4(STRLEN JTLBL4))
 (SETQ JTLG5(STRLEN JTLBL5))
 (SETQ JTLG6(STRLEN JTLBL6))
 (SETQ JTLG7(STRLEN JTLBL7))
 (SETQ JTLG8(STRLEN JTLBL8))
)

?

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 3 of 8

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

... I just need to know the length of the longest string from these variables

 

(SETQ HDRLG(STRLEN HEADER))
....

(SETQ JTLG8(STRLEN JTLBL8))

....


Or, if you don't actually need those string lengths stored into separate variables for some purpose(s) other than finding the length of the longest one, you can skip the string-length variables entirely, and get the answer straight from the string variables themselves:

 

(apply 'max
  (mapcar 'strlen
    (list HEADER THLBL JTLBL1 JTLBL2 JTLBL3 JTLBL4 JTLBL5 JTLBL6 JTLBL7 JTLBL8)
  )
)

Kent Cooper, AIA
0 Likes
Message 4 of 8

Anonymous
Not applicable

As usual you always come through with a solution, but my lack of LISP knowledge is getting the best of me again

I'm not sure how to apply your solution to a variable equal to the maximum length of those in the list

I tried

(setq elwt

  (apply 'max
    (mapcar 'strlen
      (list HEADER THLBL JTLBL1 JTLBL2 JTLBL3 JTLBL4 JTLBL5 JTLBL6 JTLBL7 JTLBL8)
    )
  )

)

but that wasn't correct

?

0 Likes
Message 5 of 8

Kent1Cooper
Consultant
Consultant
Accepted solution

@Anonymous wrote:

....

I tried

(setq elwt

  (apply 'max
    (mapcar 'strlen
      (list HEADER THLBL JTLBL1 JTLBL2 JTLBL3 JTLBL4 JTLBL5 JTLBL6 JTLBL7 JTLBL8)
    )
  )

)

but that wasn't correct

?


What does it return when you run that?  It does require that the content of all those variables are, in fact, text strings, and that they all exist [because (strlen) will have a problem trying to find the length of a non-existent string].  If it's only that they might not always all be present, it's possible to account for that:

 

(setq elwt

  (apply 'max
    (mapcar 'strlen
      (vl-remove nil (list HEADER THLBL JTLBL1 JTLBL2 JTLBL3 JTLBL4 JTLBL5 JTLBL6 JTLBL7 JTLBL8))
    )
  )

)

Kent Cooper, AIA
0 Likes
Message 6 of 8

Anonymous
Not applicable

it returns NIL

and yes there were some in the list that didn't have a value

I'm heading out now, so I'll take another look at it tomorrow

 

Thanks Kent

0 Likes
Message 7 of 8

Anonymous
Not applicable

vl-remove nil the answer!

 

Thank you again Kent

0 Likes
Message 8 of 8

john.uhden
Mentor
Mentor

I thought I had responded earlier, but this computer is *&^%$!$U&^*&^^%$^!!!

Anyway,

 

(vl-remove-if-not '(lambda (x)(= (type x) 'STR))(list ...))
  or
(vl-remove-if '(lambda (x)(/= (type x) 'STR))(list ...))

John F. Uhden

0 Likes