I've got this weird problem here :
Here's my hashtable (about Morse Code):
$replacementDecodeMap = @{
' ● ― ' = 'a'
' ― ● ● ● ' = 'b'
' ― ● ― ● ' = 'c'
' ― ● ● ' = 'd'
' ● ' = 'e'
' ● ● ― ● ' = 'f'
' ― ― ● ' = 'g'
' ● ● ● ● ' = 'h'
' ● ● ' = 'i'
' ● ― ― ― ' = 'j'
' ― ● ― ' = 'k'
' ● ― ● ● ' = 'l'
' ― ― ' = 'm'
' ― ● ' = 'n'
' ― ― ― ' = 'o'
' ● ― ― ● ' = 'p'
' ― ― ● ― ' = 'q'
' ● ― ● ' = 'r'
' ● ● ● ' = 's'
' ― ' = 't'
' ● ● ― ' = 'u'
' ● ● ● ― ' = 'v'
' ● ― ― ' = 'w'
' ― ● ● ― ' = 'x'
' ― ● ― ― ' = 'y'
' ― ― ● ● ' = 'z'
' ● ― ― ― ― ' = '1'
' ● ● ― ― ― ' = '2'
' ● ● ● ― ― ' = '3'
' ● ● ● ● ― ' = '4'
' ● ● ● ● ● ' = '5'
' ― ● ● ● ● ' = '6'
' ― ― ● ● ● ' = '7'
' ― ― ― ● ● ' = '8'
' ― ― ― ― ● ' = '9'
' ― ― ― ― ― ' = '0'
' ● ● ― ― ● ● ' = '?'
' ― ● ― ● ― ― ' = '!'
' ● ― ● ― ● ― ' = '.'
' ― ― ● ● ― ― ' = ','
' ― ● ― ● ― ● ' = "'"
' ― ― ― ● ● ● ' = ':'
' ● ― ● ― ● '= '+'
' ― ● ● ● ● ― ' = '-'
' ― ● ● ― ● ' = '÷'
' * ― ' = 'a'
' ― * * * ' = 'b'
' ― * ― * ' = 'c'
' ― * * ' = 'd'
' * ' = 'e'
' * * ― * ' = 'f'
' ― ― * ' = 'g'
' * * * * ' = 'h'
' * * ' = 'i'
' * ― ― ― ' = 'j'
' ― * ― ' = 'k'
' * ― * * ' = 'l'
' ― * ' = 'n'
' * ― ― * ' = 'p'
' ― ― * ― ' = 'q'
' * ― * ' = 'r'
' * * * ' = 's'
' * * ― ' = 'u'
' * * * ― ' = 'v'
' * ― ― ' = 'w'
' ― * * ― ' = 'x'
' ― * ― ― ' = 'y'
' ― ― * * ' = 'z'
' * ― ― ― ― ' = '1'
' * * ― ― ― ' = '2'
' * * * ― ― ' = '3'
' * * * * ― ' = '4'
' * * * * * ' = '5'
' ― * * * * ' = '6'
' ― ― * * * ' = '7'
' ― ― ― * * ' = '8'
' ― ― ― ― * ' = '9'
' * * ― ― * * ' = '?'
' ― * ― * ― ― ' = '!'
' * ― * ― * ― ' = '.'
' ― ― * * ― ― ' = ','
' ― * ― * ― * ' = "'"
' ― ― ― * * * ' = ':'
' * ― * ― * '= '+'
' ― * * * * ― ' = '-'
' ― * * ― * ' = '÷'
' ' = ' '
}
I have decomposed my message into an array, so each letter or space corresponds to a line.
Now I am trying to replace the contents of the lines according to the hastable.
This process loops round the number of times of the array :
[array]$inparts = @()
$inparts = $message.split("/")
[array]$outparts = @(0) * $inparts.Length
$ntimes = 0
do {
foreach ($e in $replacementDecodeMap) {
$in = $inparts[$ntimes]
$out = $in -replace $e.Name, $e.Value
$outparts[$ntimes] = $out
}
$ntimes = $ntimes + 1
} until (
($ntimes - 1) -eq $inparts.Length
)
Write-Host $outparts
The problem with that is that it doesn't replace anything, it just copies over the lines of the array...
I have no idea what I'm doing wrong here... Please help me.
EDIT :
Here is an example message : ― ― / ― ● ― ― // ― ● / ● ― / ― ― / ● // ● ● / ● ● ● // ● ● ● / ● ● / ● ― ● ● / ● ― ● ● / ― ― ― / ― ● ― / ― ● ― ― / (it means "my name is silloky").
PS > $inparts = $message.split("/")
PS > $inparts
― ―
●
● ●
● ● ●
● ● ●
● ●
● ― ● ●
● ― ● ●
― ― ―
― ● ―
― ● ― ―
PS > [array]$outparts = @(0) * $inparts.Length
PS > $outparts
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
The idea is to end up with (and so have the Morse code message decoded):
PS > $outparts
m
y
#that's a space here
n
a
m
e
#that's a space here
i
s
#that's a space here
s
i
l
l
o
k
y
PS > $outmessage = $outparts -join ""
PS > $outmessage
my name is silloky
#YAY, IT WORKS... no this is just me writing what the code should do...
This might not be the best way to make a Morse code decoder, but I'm supposed to build this by myself, so I even if you propose a completely different solution than mine, I will discard it (but that doesn't mean I won't be interested to read it...)
$messagevalue?foreach( $e in $replacementDecodeMap )for a hashtable doesn't enumerate the key-value pairs - instead it loops once with the hashtable itself as the loop variable. You probably wantforeach( $e in $replacementDecodeMap.GetEnumerator() ). See stackoverflow.com/a/37635938/3156906 for other options...$replacementDecodeMaphash table, then just do:$inparts.ForEach{ $replacementDecodeMap[$_ -Replace '\s'] }ynameissillokyinstead ofmy name is..., after splitting on/you can simply do[string]::new($replacementDecodeMap[$message])