Case Study CS000017:

The problem: Morse. Convert a short phrase to Morse Code

The solution:

  1. Create raw data defining codes for letters.
  2. Restructure the raw data into a table of two rows.
  3. Look up message letters in first row to index codes out of second row.
  4. Format the result into a column for each word.

Note: You can cut and paste these code fragments into the code pane of the Glee interpreter and experiment as you go along to see the actual operations live.

The Glee code:

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-----;
raw~(9 10 13 32 #asc)\|~';'@& ([.x]{(x<-)(x<-_1)})%\% =>tbl;

'Where have all the flowers gone?'%\ =>msg;

'(',|msg,|')',|(tbl[2]<[tbl[1]<** @== `msg=>i])\(i*=0)%\% %*8 ,,$;

The Output:
(w).--   (h)....  (a).-    (t)-     (f)..-.  (g)--.  
(h)....  (a).-    (l).-..  (h)....  (l).-..  (o)---  
(e).     (v)...-  (l).-..  (e).     (o)---   (n)-.   
(r).-.   (e).     ( )      ( )      (w).--   (e).    
(e).     ( )                        (e).     (?)..--.
( )                                 (r).-.           
                                    ( )              

The play-by-play:

  1. "a.-, b-..., c-.-. ..... , 0----- "=>raw;: Create a string where each letter is followed by it's Morse Code. Use the comma for the delimiter.
  2. raw~(9 10 13 32 #asc)\|~','@& ([.x]{(x<-)(x<-_1)})%\% =>tbl; : Create a table from this raw data.

    raw~(9 10 13 32 #asc): First remove white space.
    \|~';': Break into a sequence at the semicolons and "eat"(~) the breaking delimiter (semicolon).
    @& ([.x]{(x<-)(x<-_1)}): At each sequence element make a sequence of letter and code.
    %\% =>tbl;: Transpose from 2 column table to 2 row table.
  3. 'Where have all the flowers gone @ ?'%\ =>msg; : Capture the message in a variable
  4. '(',|msg,|')',|(tbl[2]<[tbl[1]<** @== `msg=>i])\(i*=0)%\% %*8 ,,$;: Look up the message and format the result.

    tbl[1]<**: Row 1 is a sequence of characters. Disclose it to a string
    `msg=>i: Find the table index for each character in the string.
    tbl[2]<[tbl[1]<** @== `msg=>i]: Use that index (@== for exact to see the ? character) to get codes out of the table's second row
    \(i*=0): Break into sequence where index is 0 (this breaks at blanks ... thus words)
    '(',|msg,|')',|(tbl[2]: Build up columns of result by column catenation.
    %\% %*8 ,,$;: Transpose the result and format each item in 7 wide field. Display the result

This completes the example. To better understand these operators and other things you can do with them, consult the operator pages according to the type of data you see being operated on.