PowerShell - instrukcja warunkowa obsługująca litery

0
$a = "abc"
if(a[0] == "a"){Write-Host "a"}
else {Write-Host "inna"}

Jak napisać instrukcje warunkową na tej zasadzie w PowerShellu.

0
if($a.StartsWith("a")) ...
if($a[0].CompareTo("a")) ...
0

Próbuje napisać program, szyfrujący szyfrem cezara.

$alphabet = "abcdefghijklmn"
$ciphertext = ""
Write-Host "PROGRAM SZYFRUJACY"
$plaintext = Read-Host "PODAJ TEKST DO ZASZYFROWANIA"
[int]$shift = Read-Host "PODAJ PRZESUNIECIE"
[int]$plaintextL = $plaintext.Length
[int]$alphabetL = $alphabet.Length

for([int]$x = 0; $x -le $plaintextL; $x++)
{
    for([int]$d = 0; $d -le $alphabetL; $d++)
    {
        if($plaintext[$x].CompareTo($alphabetL[$d])){$ciphertext[$x] = $alphabet[[int]$d+[int]$shift]}
    }
}
Write-Host "WYNIK"$ciphertext

Przy działaniu dostaje:

PS C:\Users> C:\Users\Desktop\Untitled1.ps1
PROGRAM SZYFRUJACY
Unable to index into an object of type System.Int32.
At C:\Users\Desktop\Untitled1.ps1:13 char:48

  •     if($plaintext[$x].CompareTo($alphabetL[ <<<< $d])){$ciphertext[$x] = $alphabet[[int]$d+[int]$shift]}
    
    • CategoryInfo : InvalidOperation: (0:Int32) [], RuntimeException
    • FullyQualifiedErrorId : CannotIndex

Unable to index into an object of type System.Int32.
At C:\Users\Desktop\Untitled1.ps1:13 char:48

  •     if($plaintext[$x].CompareTo($alphabetL[ <<<< $d])){$ciphertext[$x] = $alphabet[[int]$d+[int]$shift]}
    
    • CategoryInfo : InvalidOperation: (1:Int32) [], RuntimeException
    • FullyQualifiedErrorId : CannotIndex

Unable to index into an object of type System.Int32.
At C:\Users\Desktop\Untitled1.ps1:13 char:48

  •     if($plaintext[$x].CompareTo($alphabetL[ <<<< $d])){$ciphertext[$x] = $alphabet[[int]$d+[int]$shift]}
    
    • CategoryInfo : InvalidOperation: (0:Int32) [], RuntimeException
    • FullyQualifiedErrorId : CannotIndex

Unable to index into an object of type System.Int32.
At C:\Users\Desktop\Untitled1.ps1:13 char:48

  •     if($plaintext[$x].CompareTo($alphabetL[ <<<< $d])){$ciphertext[$x] = $alphabet[[int]$d+[int]$shift]}
    
    • CategoryInfo : InvalidOperation: (1:Int32) [], RuntimeException
    • FullyQualifiedErrorId : CannotIndex

WYNIK

i nie dostaje wyniku.

1 użytkowników online, w tym zalogowanych: 0, gości: 1