Page 1 sur 1

Définir un ordre de passage  

Posté : jeu. 23 mai 2019 12:09
par littlechiki
Bonjour j'ai créé une fonction pour pourvoir définir une ordre de passage en fonction d'une valeur préétablie (ici $initiative) mais ça ne marche pas. quelqu'un aurait il l'amabilité de m'expliquer mon erreur parce que là, je vois pas...

Code : Tout sélectionner

Func _Ordre($initiative1,$initiative2,$initiative3,$initiative4,$initiative5,$initiative6,$initiative7,$initiative8,$initiative9,$initiative10,$ordre1,$ordre2,$ordre3,$ordre4,$ordre5,$ordre6,$ordre7,$ordre8,$ordre9,$ordre10)
	Local $init[10] = [$initiative1,$initiative2,$initiative3,$initiative4,$initiative5,$initiative6,$initiative7,$initiative8,$initiative9,$initiative10]
	_ArraySort($init,1)
	If $init[0] = $initiative1 Then
		$ordre1 = 1
	ElseIf $init[1] = $initiative1 Then
		$ordre1 = 2
	ElseIf $init[2] = $initiative1 Then
		$ordre1 = 3
	ElseIf $init[3] = $initiative1 Then
		$ordre1 = 4
	ElseIf $init[4] = $initiative1 Then
		$ordre1 = 5
	ElseIf $init[5] = $initiative1 Then
		$ordre1 = 6
	ElseIf $init[6] = $initiative1 Then
		$ordre1 = 7
	ElseIf $init[7] = $initiative1 Then
		$ordre1 = 8
	ElseIf $init[8] = $initiative1 Then
		$ordre1 = 9
	ElseIf $init[9] = $initiative1 Then
		$ordre1 = 10
	EndIf
	 ; 2
	If $init[0] = $initiative2 Then
		$ordre2 = 1
	ElseIf $init[1] = $initiative2 Then
		$ordre2 = 2
	ElseIf $init[2] = $initiative2 Then
		$ordre2 = 3
	ElseIf $init[3] = $initiative2 Then
		$ordre2 = 4
	ElseIf $init[4] = $initiative2 Then
		$ordre2 = 5
	ElseIf $init[5] = $initiative2 Then
		$ordre2 = 6
	ElseIf $init[6] = $initiative2 Then
		$ordre2 = 7
	ElseIf $init[7] = $initiative2 Then
		$ordre2 = 8
	ElseIf $init[8] = $initiative2 Then
		$ordre2 = 9
	ElseIf $init[9] = $initiative2 Then
		$ordre2 = 10
	EndIf
	; 3
	If $init[0] = $initiative3 Then
		$ordre3 = 1
	ElseIf $init[1] = $initiative3 Then
		$ordre3 = 2
	ElseIf $init[2] = $initiative3 Then
		$ordre3 = 3
	ElseIf $init[3] = $initiative3 Then
		$ordre3 = 4
	ElseIf $init[4] = $initiative3 Then
		$ordre3 = 5
	ElseIf $init[5] = $initiative3 Then
		$ordre3 = 6
	ElseIf $init[6] = $initiative3 Then
		$ordre3 = 7
	ElseIf $init[7] = $initiative3 Then
		$ordre3 = 8
	ElseIf $init[8] = $initiative3 Then
		$ordre3 = 9
	ElseIf $init[9] = $initiative3 Then
		$ordre3 = 10
	EndIf
; 4
	If $init[0] = $initiative4 Then
		$ordre4 = 1
	ElseIf $init[1] = $initiative4 Then
		$ordre4 = 2
	ElseIf $init[2] = $initiative4 Then
		$ordre4 = 3
	ElseIf $init[3] = $initiative4 Then
		$ordre4 = 4
	ElseIf $init[4] = $initiative4 Then
		$ordre4 = 5
	ElseIf $init[5] = $initiative4 Then
		$ordre4 = 6
	ElseIf $init[6] = $initiative4 Then
		$ordre4 = 7
	ElseIf $init[7] = $initiative4 Then
		$ordre4 = 8
	ElseIf $init[8] = $initiative4 Then
		$ordre4 = 9
	ElseIf $init[9] = $initiative4 Then
		$ordre4 = 10
	EndIf
; 5
	If $init[0] = $initiative5 Then
		$ordre5 = 1
	ElseIf $init[1] = $initiative5 Then
		$ordre5 = 2
	ElseIf $init[2] = $initiative5 Then
		$ordre5 = 3
	ElseIf $init[3] = $initiative5 Then
		$ordre5 = 4
	ElseIf $init[4] = $initiative5 Then
		$ordre5 = 5
	ElseIf $init[5] = $initiative5 Then
		$ordre5 = 6
	ElseIf $init[6] = $initiative5 Then
		$ordre5 = 7
	ElseIf $init[7] = $initiative5 Then
		$ordre5 = 8
	ElseIf $init[8] = $initiative5 Then
		$ordre5 = 9
	ElseIf $init[9] = $initiative5 Then
		$ordre5 = 10
	EndIf
; 6
	If $init[0] = $initiative6 Then
		$ordre6 = 1
	ElseIf $init[1] = $initiative6 Then
		$ordre6 = 2
	ElseIf $init[2] = $initiative6 Then
		$ordre6 = 3
	ElseIf $init[3] = $initiative6 Then
		$ordre6 = 4
	ElseIf $init[4] = $initiative6 Then
		$ordre6 = 5
	ElseIf $init[5] = $initiative6 Then
		$ordre6 = 6
	ElseIf $init[6] = $initiative6 Then
		$ordre6 = 7
	ElseIf $init[7] = $initiative6 Then
		$ordre6 = 8
	ElseIf $init[8] = $initiative6 Then
		$ordre6 = 9
	ElseIf $init[9] = $initiative6 Then
		$ordre6 = 10
	EndIf
; 7
	If $init[0] = $initiative7 Then
		$ordre7 = 1
	ElseIf $init[1] = $initiative7 Then
		$ordre7 = 2
	ElseIf $init[2] = $initiative7 Then
		$ordre7 = 3
	ElseIf $init[3] = $initiative7 Then
		$ordre7 = 4
	ElseIf $init[4] = $initiative7 Then
		$ordre7 = 5
	ElseIf $init[5] = $initiative7 Then
		$ordre7 = 6
	ElseIf $init[6] = $initiative7 Then
		$ordre7 = 7
	ElseIf $init[7] = $initiative7 Then
		$ordre7 = 8
	ElseIf $init[8] = $initiative7 Then
		$ordre7 = 9
	ElseIf $init[9] = $initiative7 Then
		$ordre7 = 10
	EndIf
; 8
	If $init[0] = $initiative8 Then
		$ordre8 = 1
	ElseIf $init[1] = $initiative8 Then
		$ordre8 = 2
	ElseIf $init[2] = $initiative8 Then
		$ordre8 = 3
	ElseIf $init[3] = $initiative8 Then
		$ordre8 = 4
	ElseIf $init[4] = $initiative8 Then
		$ordre8 = 5
	ElseIf $init[5] = $initiative8 Then
		$ordre8 = 6
	ElseIf $init[6] = $initiative8 Then
		$ordre8 = 7
	ElseIf $init[7] = $initiative8 Then
		$ordre8 = 8
	ElseIf $init[8] = $initiative8 Then
		$ordre8 = 9
	ElseIf $init[9] = $initiative8 Then
		$ordre8 = 10
	EndIf
; 9
	If $init[0] = $initiative9 Then
		$ordre9 = 1
	ElseIf $init[1] = $initiative9 Then
		$ordre9 = 2
	ElseIf $init[2] = $initiative9 Then
		$ordre9 = 3
	ElseIf $init[3] = $initiative9 Then
		$ordre9 = 4
	ElseIf $init[4] = $initiative9 Then
		$ordre9 = 5
	ElseIf $init[5] = $initiative9 Then
		$ordre9 = 6
	ElseIf $init[6] = $initiative9 Then
		$ordre9 = 7
	ElseIf $init[7] = $initiative9 Then
		$ordre9 = 8
	ElseIf $init[8] = $initiative9 Then
		$ordre9 = 9
	ElseIf $init[9] = $initiative9 Then
		$ordre9 = 10
	EndIf
; 10
	If $init[0] = $initiative10 Then
		$ordre10 = 1
	ElseIf $init[1] = $initiative10 Then
		$ordre10 = 2
	ElseIf $init[2] = $initiative10 Then
		$ordre10 = 3
	ElseIf $init[3] = $initiative10 Then
		$ordre10 = 4
	ElseIf $init[4] = $initiative10 Then
		$ordre10 = 5
	ElseIf $init[5] = $initiative10 Then
		$ordre10 = 6
	ElseIf $init[6] = $initiative10 Then
		$ordre10 = 7
	ElseIf $init[7] = $initiative10 Then
		$ordre10 = 8
	ElseIf $init[8] = $initiative10 Then
		$ordre10 = 9
	ElseIf $init[9] = $initiative10 Then
		$ordre10 = 10
	EndIf
EndFunc
Merci à l'avance pour votre aide !

Re: Définir un ordre de passage

Posté : jeu. 23 mai 2019 12:51
par jchd
Il serait bien plus simple de passer un tableau à la fonction plutôt que 10 arguments.
Ensuite il serait encore plus simple de créer ce tableau en 2D avec valeur (initiativeX) et index. Le seul tri sur valeur résoud la question.
On évite ainsi 100 tests et 21 variables inutiles.

Re: Définir un ordre de passage

Posté : jeu. 23 mai 2019 13:36
par littlechiki
Là, je dois admettre que j'ai pas compris grand chose. désolé pour ça mais est-ce que je pourrais avoir un exemple ? Je pense que ça m'aiderait grandement, merci !

Re: Définir un ordre de passage

Posté : jeu. 23 mai 2019 14:55
par jchd

Code : Tout sélectionner

Local $aInitiatives = [ _
	7, _		; ($initiative1)
	23, _		; ($initiative2)
	4, _		; ($initiative3)
	-3, _		; ($initiative4)
	11, _		; ($initiative5)
	49, _		; ($initiative6)
	8, _		; ($initiative7)
	-17, _		; ($initiative8)
	6, _		; ($initiative9)
	14 _		; ($initiative10)
]

_ArrayColInsert($aInitiatives, 1)
For $i = 0 To UBound($aInitiatives) - 1
	$aInitiatives[$i][1] = $i + 1
Next
_ArrayDisplay($aInitiatives, "Non trié")
_ArraySort($aInitiatives, 1)
_ArrayDisplay($aInitiatives, "Trié")

Re: Définir un ordre de passage

Posté : jeu. 23 mai 2019 16:43
par littlechiki
Alors je viens d'adapter la fonction à mon application et ça marche avec une erreur, celle ci étant que les chiffres de unités sont considérés comme plus haut que les dizaines. exemple, 8 sera supérieur à 76 mais inférieur à 9 qui lui même sera supérieur à 83. Je ne sais pas si c'est parce que je rentre moi même dans l'application les valeur de $initiative mais ça pose problème...

Re: Définir un ordre de passage

Posté : jeu. 23 mai 2019 16:45
par littlechiki
Cependant si je prend des valeurs de $initiative que j'ai établi avant de lancer l'application, il n'y a aucun problème.

Re: Définir un ordre de passage

Posté : jeu. 23 mai 2019 18:08
par jchd
Convertir les chaînes en nombre avec Number() ou Int() résoudra le problème.
35 > 4
mais
'35' < '4'

Re: Définir un ordre de passage

Posté : jeu. 23 mai 2019 19:16
par littlechiki
Ah d'accord, ça j'ai compris ! J'ai réussi à réglé le problème en utilisant un number pour dire $initiative = Number($initiative). Il y a probablement plus simple mais ça marche ! Merci pour toute cette aide !

Re: Définir un ordre de passage

Posté : jeu. 23 mai 2019 20:40
par amphase
Bonsoir,
Quand les nombres sont traités comme des chiennes chaînes...
Le pourquoi et le comment :
#include <Array.au3>

; valeurs numériques dans la 1ère colonne
Global $aIniOrd = [[85, "f1"], [7, "f2"], [600, "fn"]]
_DispSortDisp($aIniOrd)

; chaînes de caractères
Global $aIniOrd = [["85", "f1"], ["7", "f2"], ["600", "fn"]]
_DispSortDisp($aIniOrd)

; chaînes de caractères formatées avec des zéros devant pour avoir une longueur constante de 3 caractères
Global $sZeros = "000" ; correspond à 0
For $i = 0 To UBound($aIniOrd) - 1
   $iLen = StringLen($aIniOrd[$i][0]) ; nbre de caractères dans la chaîne
;~    ConsoleWrite(@LF & "L16 --> " & $iLen & @LF)
   $aIniOrd[$i][0] = StringLeft($sZeros, 3 - $iLen) & $aIniOrd[$i][0]
;~    ConsoleWrite(@LF & "L18 --> " & StringLeft($sZeros, 3 - $iLen) & @LF)
Next
_DispSortDisp($aIniOrd)


Func _DispSortDisp($a) ; affiche, trie, affiche
   _ArrayDisplay($a)
   _ArraySort($a)
   _ArrayDisplay($a)
EndFunc