Page 1 sur 1
[..] Comparer 2 variables
Posté : mer. 13 avr. 2011 18:27
par ethneldryt
Bonjour, je demande car je ne trouve pas de fonction string pour faire cela.
$var1 = "ABCDE"
$var2 = "ACDE"
Il y a 2 vars, dans la deuxième, il y a le B en moin, je cherche donc a afficher ce qui a disparue, du genre :
$resultat = fonction("ABCDE","ACDE")
La variable $resultat contiendrai "B".
Merci
Re: [..] Comparer 2 variables
Posté : mer. 13 avr. 2011 19:53
par jchd
Si tu ne tiens compte que des suppressions de lettres, c'est éventuellement jouable avec une ou plutôt des regexp.
Par contre si tu envisage toutes les possibilités (manque, ajout, échange, interversion) alors là je te conseille d'étudier la fonction ci-dessous et de l'adapter à ton besoin particulier. Désolé pour mes commentaires en anglais.
D'une manière générale, la comparaison de chaînes est un problème complexe.
► Afficher le texte
Code : Tout sélectionner
; Computes the number of typos (Damerau-Levenshtein distance) between two strings.
; Four types of differences are counted:
; insertion of a character, abcd ab#cd
; deletion of a character, abcd acd
; exchange of a character abcd ab$d
; inversion of adjacent chars abcd acbd
;
; This function does NOT satisfy the so-called "triangle inequality", which means
; more simply that it makes NO attempt to compute the MINIMUM edit distance in all
; cases. If you need that, you should use more complex algorithms.
;
; This simple function allows a fuzzy compare for e.g. recovering from typical
; human typos in short strings like names, address, cities... while getting rid of
; minor scripting differences (accents, ligatures).
;
; Strings are unaccented then lowercased.
; String $st2 can be used as a pattern similar to the SQL 'LIKE' operator:
; '_' and trailing '%' act as in LIKE. These wildcards can be passed as parameters
; but should contain exactly one character for the function to work properly.
Func _Typos(Const $st1, Const $st2, $anychar = '_', $anytail = '%')
Local $s1, $s2, $pen, $del, $ins, $subst
If Not IsString($st1) Then Return SetError(-1, -1, -1)
If Not IsString($st2) Then Return SetError(-2, -2, -1)
If $st2 = '' Then Return StringLen($st1)
If $st2 == $anytail Then Return 0
If $st1 = '' Then
Return(StringInStr($st2 & $anytail, $anytail, 1) - 1)
EndIf
$s1 = StringSplit(_LowerUnaccent($st1), "", 2)
$s2 = StringSplit(_LowerUnaccent($st2), "", 2)
Local $l1 = UBound($s1), $l2 = UBound($s2)
Local $r[$l1 + 1][$l2 + 1]
For $x = 0 To $l2 - 1
Switch $s2[$x]
Case $anychar
If $x < $l1 Then
$s2[$x] = $s1[$x]
EndIf
Case $anytail
$l2 = $x
If $l1 > $l2 Then
$l1 = $l2
EndIf
ExitLoop
EndSwitch
$r[0][$x] = $x
Next
$r[0][$l2] = $l2
For $x = 0 To $l1
$r[$x][0] = $x
Next
For $x = 1 To $l1
For $y = 1 To $l2
$pen = Not ($s1[$x - 1] == $s2[$y - 1])
$del = $r[$x-1][$y] + 1
$ins = $r[$x][$y-1] + 1
$subst = $r[$x-1][$y-1] + $pen
If $del > $ins Then $del = $ins
If $del > $subst Then $del = $subst
$r[$x][$y] = $del
If ($pen And $x > 1 And $y > 1 And $s1[$x-1] == $s2[$y-2] And $s1[$x-2] == $s2[$y-1]) Then
If $r[$x][$y] >= $r[$x-2][$y-2] Then $r[$x][$y] = $r[$x-2][$y-2] + 1
$r[$x-1][$y-1] = $r[$x][$y]
EndIf
Next
Next
Return ($r[$l1][$l2])
EndFunc
Re: [..] Comparer 2 variables
Posté : mer. 13 avr. 2011 20:09
par ethneldryt
Salut et merci. en effet, il y aura qu'une lettre qui disparaitra a chaque fois, mais je ne sais comment faire.
Re: [..] Comparer 2 variables
Posté : mer. 13 avr. 2011 21:06
par sylvanie
Dans ce cas on peut envisager ceci
► Afficher le texte
Code : Tout sélectionner
$var1 = "ABCDE"
$var2 = "ACDE"
$diff=lettre_diff($var1,$var2)
MsgBox(0,"Différence",$diff)
Func lettre_diff($ch1,$ch2)
If StringLen($ch1)<StringLen($ch2) Then Return lettre_diff($ch2,$ch1)
Return StringRegExpReplace($ch1,"["&$ch2&"]","")
EndFunc
Re: [..] Comparer 2 variables
Posté : mer. 13 avr. 2011 23:09
par jchd
Attention sylvanie, tu éjectes un _set_ de la chaîne sujet. Tu présupposes qu'il n'y a pas de répétition possible dans l'entrée, ce qui n'est pas forcément adapté dans le cas présent.
et la suppression du second A de $var1 passe inaperçue. Oooups ou pas, c'est à ethneldryt de le dire.
Sinon, on fait ça :
Code : Tout sélectionner
ConsoleWrite('On a perdu un ' & yaplus('ABCABCDEF', 'ABCBCDEF') & @LF)
Func yaplus($sRef, $sTest)
Local $sMatch, $iLen = StringLen($sTest)
For $i = 1 To $iLen
If StringLeft($sRef, $i) <> StringLeft($sTest, $i) Then ExitLoop
Next
Return StringMid($sRef, $i, 1)
EndFunc
Re: [..] Comparer 2 variables
Posté : jeu. 14 avr. 2011 10:12
par sylvanie
Tout à fait d'accord.
D'après ce qui était exposé, j'avais effectivement compris que les chaînes ne comportaient pas de répétition.
Re: [..] Comparer 2 variables
Posté : jeu. 14 avr. 2011 11:50
par jchd
Tu sais, à mon âge avancé de soixantenaire aigri je suis presque liquide et devenu méfiant de tout
