Pour comprendre l'intérêt de l'utilisation des tableaux, prenons un exemple concret:
lire les 10 premières lignes d'un fichier texte.
| Sans tableau |
Avec tableau |
|
OPEN fichier$ FOR INPUT AS #1
INPUT #1, ligne1$
INPUT #1, ligne2$
INPUT #1, ligne3$
INPUT #1, ligne4$
INPUT #1, ligne5$
INPUT #1, ligne6$
INPUT #1, ligne7$
INPUT #1, ligne8$
INPUT #1, ligne9$
INPUT #1, ligne10$
CLOSE #1
|
DIM ligne$(10)
OPEN fichier$ FOR INPUT AS #1
FOR i = 1 TO 10
INPUT #1, ligne$(i)
NEXT i
CLOSE #1
|
PREMIER AVANTAGE: nous pouvons automatiser les procéssus car toutes les lignes sont stockées dans un même tableau. Si par exemple i = 1, on veux avoir la ligne 1 et i = 2, la ligne 2, cette valeur est la même: ligne$(i). Ce qui est impossible par utilisation
des variables classiques.
SECOND AVANTAGE: notre premier exemple donne 12 lignes classiques contre 6 avec un tableau, ce n'est pas bien méchant. Mais si nous voulons lire 1000 lignes, cette fois-ci:
| Sans tableau |
Avec tableau |
|
OPEN fichier$ FOR INPUT AS #1
INPUT #1, ligne1$
INPUT #1, ligne2$
INPUT #1, ligne3$
=> répetition de la ligne <=
INPUT #1, ligne1000$
CLOSE #1
|
DIM ligne$(10000)
OPEN fichier$ FOR INPUT AS #1
FOR i = 1 TO 10000
INPUT #1, ligne$(i)
NEXT i
CLOSE #1
|
Sans tableau, cela nous fait 1002 lignes, mais toujours 6 avec un tableau! Et là ce
n'est plus négligeable!
Pour revenir à mon premier constat, je dirais qu'il est absurde d'envisager de
manipuler un grand nombre de variables sans l'aide d'un tableau.