0

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...)

6
  • Can you show us a sample $message value? Commented Jul 13, 2022 at 9:05
  • 1
    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 want foreach( $e in $replacementDecodeMap.GetEnumerator() ). See stackoverflow.com/a/37635938/3156906 for other options... Commented Jul 13, 2022 at 9:36
  • Looks to me that the iteration isn't even required: Remove all the spaces from the names of the given $replacementDecodeMap hash table, then just do: $inparts.ForEach{ $replacementDecodeMap[$_ -Replace '\s'] } Commented Jul 13, 2022 at 10:21
  • Do you mind writing answers with code blocks, it might be easier for me, thanks ! Commented Jul 13, 2022 at 11:20
  • your message gives me ynameissilloky instead of my name is..., after splitting on / you can simply do [string]::new($replacementDecodeMap[$message]) Commented Jul 13, 2022 at 12:58

1 Answer 1

1

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
 

Sign up to request clarification or add additional context in comments.

10 Comments

Thanks for the reply; to be honest i have no idea what you're talking about, so i might also look under the grey veil...
$inparts = $message.split("/"); foreach( $inpart in $inparts ) { # convert this part into a letter }
Yeah, would never have been able to get to that myself : still haven't completely understood piping...
What does this mean : $code = $_; ?
The pipeline isn't the key here - you could do it with a foreach loop as well. The main difference is looping over $inpart rather than $replacementDecodeMap.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.