Page 1 sur 1
[..] Incrementer le nom d'une variable
Posté : lun. 18 août 2014 16:51
par BarbaMoi
Bonjour,
Je voudrais intégrer dans mon code des nom de variables dont la fin serait incrémentée par une boucle : Var1, Var2, Var3... J'ai trouv des exemples mais ça ne fonctionne pas dans mon script. En fait, j'ai un fichier ini qui comporte 5 variables qui contiennent un chemin de fichier.
Fichier ini :
► Afficher le texte
Code : Tout sélectionner
[Paths]
#Chemin Fichier 1
PathFichier1 = C:\Fichier1.txt
#Chemin Fichier 2
PathFichier2 = C:\Fichier2.txt
#Chemin Fichier 3
PathFichier3 = C:\Fichier3.txt
#Chemin Fichier 4
PathFichier4 = C:\Fichier4.txt
#Chemin Fichier 5
PathFichier5 = C:\Fichier5.txt
Je veux remplir ce fichier ini par le biais d'une GUI avec des InputBox. Pour que ce soit souple et clair au niveau du script, je veux faire une boucle qui incrémente le nom de la variable :
► Afficher le texte
Code : Tout sélectionner
Global $Input_Path1 = GUICtrlCreateInput("Chemin Fichier 1", 70, 24, 449, 21)
For $i = 1 TO 5
$GUI_PathFichier = GUICtrlRead ( $Input_Path[$i] )
MsgBox ( 0, "InputPath " & $i, "Chemin Fichier " & $i & " : " & $GUI_PathFichier, 1 )
IniWrite ( $iniFile, $iniPaths, "PathFichier" & $i, " " & $GUI_PathFichier )
Next
Le problème vient du GUICtrlRead qui me remonte déjà une valeur 0 et pas le chemin.
Merci pour votre aide.
Re: [..] Incrementer le nom d'une variable
Posté : lun. 18 août 2014 17:24
par mikell
C'est tarabiscoté ton histoire, tu ferais mieux de passer par une array
Mais sinon ça peut se faire (mais c'est pas top)
Teste ça
Code : Tout sélectionner
For $i = 1 to 5
$var = IniRead("1.ini", "Paths", "PathFichier" & $i, "")
Assign("PathFichier" & $i, $var)
Next
MsgBox ( 0, "$PathFichier3", "Chemin Fichier 3" & " : " & $PathFichier3 )
For $i = 1 to 5
$var = Eval("PathFichier" & $i)
MsgBox ( 0, "InputPath " & $i, "Chemin Fichier " & $i & " : " & $var)
Next
Re: [..] Incrementer le nom d'une variable
Posté : lun. 18 août 2014 17:32
par Faco
Salut,
ça ça marche pas "$Input_Path[$i]" pour incrémenter le nom de ta variable, là tu utilise un accès à un tableau '[$i]'. mais l'idée tu peut la garder

avec un truc du genre :
► Afficher le texte
Code : Tout sélectionner
Global $Input_Path1 = GUICtrlCreateInput("Chemin Fichier 1", 70, 24, 449, 21)
Global $Input_Path2 = GUICtrlCreateInput("Chemin Fichier 2", 70, 54, 449, 21)
Global $Input_Path3 = GUICtrlCreateInput("Chemin Fichier 3", 70, 84, 449, 21)
Global $Input_Path4 = GUICtrlCreateInput("Chemin Fichier 4", 70, 114, 449, 21)
Global $Input_Path5 = GUICtrlCreateInput("Chemin Fichier 5", 70, 144, 449, 21)
Dim $tabInput_Path[5]=[$Input_Path1,$Input_Path2,$Input_Path3,$Input_Path4,$Input_Path5]
For $i = 1 TO 5
$GUI_PathFichier = GUICtrlRead ( $tabInput_Path[$i-1] ) ; -1 car le tableau commence a 0 pas 1
MsgBox ( 0, "InputPath " & $i, "Chemin Fichier " & $i & " : " & $GUI_PathFichier, 1 )
IniWrite ( $iniFile, $iniPaths, "PathFichier" & $i, " " & $GUI_PathFichier )
Next
Re: [..] Incrementer le nom d'une variable
Posté : lun. 18 août 2014 17:40
par jchd
Comme le dit mikell, c'est vraiment des solutions de bouts de ficelle. Pourquoi se compliquer la vie avec des sacs de nouilles comme ça, c'est vraiment incompréhensible pour moi.
Re: [..] Incrementer le nom d'une variable
Posté : lun. 18 août 2014 17:51
par Faco
Bon, j'ai pas du comprendre la question alors. pour moi ce qui n'allais pas c'était son "GUICtrlRead ( $Input_Path[$i] )"
Re: [..] Incrementer le nom d'une variable
Posté : lun. 18 août 2014 18:45
par orax
BarbaMoi a écrit :Le problème vient du GUICtrlRead qui me remonte déjà une valeur 0 et pas le chemin.
Ajoute
avant les GUICtrlCreateInput.
Dans le script final, les chemins des fichiers pourront être modifiés par l'utilisateur dans les
Input, n'est-ce pas ? Dans le cas contraire je ne vois pas l'intérêt des Input.
Re: [..] Incrementer le nom d'une variable
Posté : lun. 18 août 2014 21:52
par mikell
@Faco
C'est pas le problème. En s'en tenant strictement au titre du sujet, j'ai répondu qu'incrémenter un nom de variable, c'est possible mais c'est chiant et c'est un mauvais plan
Evidemment, si le titre avait été "mettre à jour un ini avec des inputs" ma réponse eût été différente

Re: [..] Incrementer le nom d'une variable
Posté : lun. 18 août 2014 22:44
par jchd
Oui, moi aussi j'ai peut-être bazooké avant de disséquer. Fallait pas employer des mots qui fâchent, suis un dino moué.
Re: [..] Incrementer le nom d'une variable
Posté : lun. 18 août 2014 23:19
par mikell
jc hard dino

Re: [..] Incrementer le nom d'une variable
Posté : lun. 18 août 2014 23:43
par orax
C'est sûr qu'avec près de 60 balais c'est le dinosaure du forum !

Re: [..] Incrementer le nom d'une variable
Posté : mar. 19 août 2014 00:57
par jchd
Hola, pas encore 60 hein
