Le tableau de Powershell et Perl
powershell_5F00_2
Une petite comparaison entre Perl et powershell concernant les tableaux simples :
Dans l’exemple on créé un tableau toto qui contient a b et c. On l’affiche, puis on ajoute a la fin du tableau une autre valeur, d.
Mise a part la syntaxe le résultat est quasiment le même.
perl:
C:\>perl -e “@toto=(“a”,”b”,”c”); print @toto; push(@toto,”d”); print @toto;”
résultat=> abcabcd
Powershell
PS C:\> $toto=(“a”,”b”,”c”); write-host $toto; $toto+=”d”;write-host $toto;
résultat=> a b c
résultat=> a b c d
Si on force l’insertion de la valeur en lui donnant la position dans le tableau Powershell n’est pas contant :
Powershell:
PS C:\> $i=(“a”,”b”,”c”); write-host $i; $i+=”d”; $i[4]=”e”;`
write-host $i;
a b c
L’affectation du tableau a échoué, car l’index « 4 » était hors limites.
Au niveau de ligne : 1 Caractère : 46
+ $i=(“a”,”b”,”c”); write-host $i; $i+=”d”; $i[ < <<< 4]="e"; write-host $i;
+ CategoryInfo : InvalidOperation: (4:Int32) [], RuntimeException
+ FullyQualifiedErrorId : IndexOutOfRange
résultat => a b c d
PS C:\>
Alors qu’en Perl c’est possible.
C:\>perl -e ”
@i=(“a”,”b”,”c”); print @i; push(@i,”d”); $i[4]=”e”;print @i;”
résultat=> abcabcde