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...)
CodePudding user response:
Here's a hint for a simpler solution - instead of looping over all the entries in your map, break the message down into individual parts like you've already done, but then use a for
loop over those and look them up one by one in the map to find which character they represent.
You'll need to add handling for converting the empty string into a space as well.
Adding spoiler tags in case you want to try that approach yourself before you peek :-)...
$message = " ― ― / ― ● ― ― // ― ● / ● ― / ― ― / ● // ● ● / ● ● ● // ● ● ● / ● ● / ● ― ● ● / ● ― ● ● / ― ― ― / ― ● ― / ― ● ― ― /"; $parts = $message.Split("/"); $letters = $parts | foreach-object { $item = $_; if( $item -eq [string]::Empty ) { write-output " "; } elseif( -not $replacementDecodeMap.ContainsKey($item) ) { throw "code '$item' isn't defined in the map"; } write-output $replacementDecodeMap[$item]; } $letters -join "" # my name is silloky