Page 1 of 1

Find highest number between several strings

Posted: 27 Nov 2019 05:03
by Lucy
How do i go about finding the highest number between several different strings? I understand basic (if more than, if less than, equals, not, etc) but have absolutely no idea how to find the highest number

Re: Find highest number between several strings

Posted: 27 Nov 2019 18:37
by Desmanto
Highest number from strings? Maybe you mean from list of numbers?

If it is from list of number, there are several way to do it. The quickest one is to sort the list and then take the last element. But I have to specify the lenght-1 in the index, and I prefer to keep the index cleaner. So i prefer to reverse it again and take index 0 instead.

Code: Select all

list = newList(3, 42, 17,15, 35);
max = reverse(sort(list))[0];
This is method is destructive, as it changes the order of the number in the list. If you need to retain the original list order, you have to copyList() first.

Code: Select all

list = newList(3, 42, 17,15, 35);
max = reverse(sort(copyList(list)))[0];
This way, the list order is maintainted.

If you need to get the index of the max number, you have to loop upon the list and compare each of them. It is a bit longer and maybe not what you need here.

Re: Find highest number between several strings

Posted: 27 Nov 2019 18:58
by Lucy
Sorry i worded that terribly.
I have a log file that consists of a fair bit of data, mostly alphabetical. However parts of the log contain numeric data. I am trying to find the highest of the numeric data stored in that file without reading it.
Hope this is more understanding

Re: Find highest number between several strings

Posted: 28 Nov 2019 17:41
by Desmanto
The number all are normal numbers without comma/dot ? If yes, then it can be done easily with simple regex.

Code: Select all

s = "The answer is 42. 3 pineapple. 87 group";
f = findAll(s, "\\d+");
max = reverse(sort(f));
replace the s with the string you want to find the highest number.

Re: Find highest number between several strings

Posted: 29 Nov 2019 00:12
by Lucy
Hmm i think they are all straight forward numbers... i will have a go at this. Thank you