mIRC Home    About    Download    Register    News    Help

Print Thread
#68059 15/01/04 09:38 PM
Joined: Dec 2003
Posts: 261
M
milosh Offline OP
Fjord artisan
OP Offline
Fjord artisan
M
Joined: Dec 2003
Posts: 261
Good morning/day/evening.

Is there a function that will compare two strings and return the longest string that's in both of them. example :

/set %str01 anything
/set %str02 everything

/echo -a The longest string in both %str01 and %str01 is $function(%str01,%str02)
;Returns: The longest string in anything and everything is ything

thanx!
Note: Maybe I can script that function, but it will not be the fastest and the shortes, that's for sure. grin


velicha dusha moja Gospoda
#68060 15/01/04 09:50 PM
Joined: Oct 2003
Posts: 273
E
EVH Offline
Fjord artisan
Offline
Fjord artisan
E
Joined: Oct 2003
Posts: 273
Code:
//echo -a The longest string in both %str01 and %str01 is $iif($len(%str01) > $len(%str02),%str01,%str02)
or
//echo -a The longest string in both %str01 and %str01 is $len($iif($len(%str01) > $len(%str02),%str01,%str02)) characters


/help $len

Last edited by EVH; 15/01/04 09:51 PM.
#68061 15/01/04 10:33 PM
Joined: Dec 2003
Posts: 261
M
milosh Offline OP
Fjord artisan
OP Offline
Fjord artisan
M
Joined: Dec 2003
Posts: 261
Oh man, my english is pure, maybe I can't explain the problem. (sorry for that).
I don't want to compare the length of two strings... I want to find the string that is in %str01 and in %str02, the longest string that's IN both these strings. (example above : "anything" "everything" the longest in both is "ything")

Sorry for my pure english.


velicha dusha moja Gospoda
#68062 16/01/04 12:13 AM
Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
There is no built-in function for that, but you can let regex do the most of the work for you. The following $regex() captures all common substrings between the two strings, making sure that all substrings have the maximum length possible ("greedy" matches). The only thing we need to do then is loop through $regml() to find the longest item in the match list. The speed shouldn't be an issue here, as that list is usually small (for small strings, usually less than 10).
Code:
alias lcs {
  if $regex($+($1,$cr,$2),/(.+)(?=.*\r.*\1)/g) { 
    var %i = $ifmatch, %a = 0
    while %i {
      if $len($regml(%i)) > %a { var %a = $ifmatch, %b = %i }
      dec %i
    }
    return $regml(%b)
  }
}
Usage: $lcs(string1,string2) , returns the longest common string between string1 and string2.


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
#68063 16/01/04 04:06 PM
Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
I made a mistake in my previous post, as I used a consuming pattern (.+), while it should all have been inside the assertion. Here's the (hopefully) correct code:
Code:
alias lcs2 {
  if $regex($+($1,$cr,$2),/(?=(.+).*\r.*\1)/g) { 
    var %i = $ifmatch, %a = 0
    while %i {
      if $len($regml(%i)) > %a { var %a = $ifmatch, %b = %i }
      dec %i
    }
    return $regml(%b)
  }
}


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
#68064 16/01/04 08:51 PM
Joined: Dec 2003
Posts: 261
M
milosh Offline OP
Fjord artisan
OP Offline
Fjord artisan
M
Joined: Dec 2003
Posts: 261
Thanx, man. I don't understand the code, but I will give my best cool


velicha dusha moja Gospoda
#68065 17/01/04 05:01 AM
Joined: Sep 2003
Posts: 4,230
D
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
Quote:

/help $len
(and above that)


lol, what drugs were you on when you read what he wanted.

PS above was said in light jeast.

#68066 17/01/04 09:13 PM
Joined: Oct 2003
Posts: 273
E
EVH Offline
Fjord artisan
Offline
Fjord artisan
E
Joined: Oct 2003
Posts: 273
It looked to me like he wanted to compare the length
of %str01 & %str02 and return the longest .. my bad.

I guess in the future I should put on my gibberish
spectacles when reading some of these posts.


Link Copied to Clipboard