<head>
 <body>
  <toc>
   <these>
    <get>
     <stripped>
      <h1 class="title">columnize</h1>
      <div id="mainSection">
       <table>
        <col style="width: 5%"/>
        <tr>
         <td>
          <b>Definition:</b>
         </td>
         <td>
         <a href="sequence.htm">sequence</a> res = columnize(<a href="sequence.htm">sequence</a> source, <a href="object.htm">object</a> columns={}, <a href="object.htm">object</a> defval=0)
         </td>
        </tr>
        <tr>
         <td>
          <b>Description:</b>
         </td>
         <td>Convert a set of <a href="sequence.htm">sequences</a> into a set of columns.
          <br>
             Any atoms found in source are treated as if they were a 1-element <a href="sequence.htm">sequence</a>.
          <br>
             The optional columns parameter can be a specific column number or an ordered set.
          <br>
             The default value is used when some elements of source have different lengths.
          <br>
          <br>
            Returns a sequence of sequences (or strings), one for each required column.
         </td>
        </tr>
        <tr>
         <td>
          <a href="p2js.htm"><span class="pwap2js"><b>pwa/p2js:</b></span></a>
         </td>
         <td>
          Supported.
         </td>
        </tr>
        <tr>
         <td>
          <b>Comments:</b>
         </td>
         <td>
            Can make it much easier to declare related items yet still be able to use a plain <a href="find.htm">find</a>().
            <br>
            <br>
            To obtain a different view/ordering of the results of columnize, a tagsort is recommended.
         </td>
        </tr>
        <tr>
         <td>
          <b>Example 1:</b>
         </td>
         <td>Perform a standard table inversion or just pick out the desired items
         </td>
        </tr>
        <tr>
         <td></td>
         <td>
<!--eucode>
?columnize({{1, 2}, {3, 4}, {5, 6}})            -- {{1,3,5}, {2,4,6}}
?columnize({{1, 2}, {3, 4}, {5, 6, 7}})         -- {{1,3,5}, {2,4,6}, {0,0,7}}
?columnize({{1}, {2}, {3, 4}},defval:=-999)     -- {{1,2,3}, {-999,-999,4}}
?columnize({{1, 2}, {3, 4}, {5, 6, 7}}, 2)      -- {{2,4,6}}
?columnize({{1, 2}, {3, 4}, {5, 6, 7}}, {2,1})  -- {{2,4,6}, {1,3,5}}
</eucode-->
<pre>
<font color="#000000"></font><font color="#0000FF">?</font><font color="#5E005E">columnize</font><font color="#0000FF">(</font><font color="#800000">{</font><font color="#800080">{</font><font color="#000000">1</font><font color="#0000FF">,</font><font color="#000000"> 2</font><font color="#800080">}</font><font color="#0000FF">,</font><font color="#800080"> {</font><font color="#000000">3</font><font color="#0000FF">,</font><font color="#000000"> 4</font><font color="#800080">}</font><font color="#0000FF">,</font><font color="#800080"> {</font><font color="#000000">5</font><font color="#0000FF">,</font><font color="#000000"> 6</font><font color="#800080">}</font><font color="#800000">}</font><font color="#0000FF">)           </font><font color="#000080"><i> -- {{1,3,5}, {2,4,6}}
</i></font><font color="#0000FF">?</font><font color="#5E005E">columnize</font><font color="#0000FF">(</font><font color="#800000">{</font><font color="#800080">{</font><font color="#000000">1</font><font color="#0000FF">,</font><font color="#000000"> 2</font><font color="#800080">}</font><font color="#0000FF">,</font><font color="#800080"> {</font><font color="#000000">3</font><font color="#0000FF">,</font><font color="#000000"> 4</font><font color="#800080">}</font><font color="#0000FF">,</font><font color="#800080"> {</font><font color="#000000">5</font><font color="#0000FF">,</font><font color="#000000"> 6</font><font color="#0000FF">,</font><font color="#000000"> 7</font><font color="#800080">}</font><font color="#800000">}</font><font color="#0000FF">)        </font><font color="#000080"><i> -- {{1,3,5}, {2,4,6}, {0,0,7}}
</i></font><font color="#0000FF">?</font><font color="#5E005E">columnize</font><font color="#0000FF">(</font><font color="#800000">{</font><font color="#800080">{</font><font color="#000000">1</font><font color="#800080">}</font><font color="#0000FF">,</font><font color="#800080"> {</font><font color="#000000">2</font><font color="#800080">}</font><font color="#0000FF">,</font><font color="#800080"> {</font><font color="#000000">3</font><font color="#0000FF">,</font><font color="#000000"> 4</font><font color="#800080">}</font><font color="#800000">}</font><font color="#0000FF">,</font><font color="#000000">defval</font><font color="#0000FF">:=-</font><font color="#000000">999</font><font color="#0000FF">)    </font><font color="#000080"><i> -- {{1,2,3}, {-999,-999,4}}
</i></font><font color="#0000FF">?</font><font color="#5E005E">columnize</font><font color="#0000FF">(</font><font color="#800000">{</font><font color="#800080">{</font><font color="#000000">1</font><font color="#0000FF">,</font><font color="#000000"> 2</font><font color="#800080">}</font><font color="#0000FF">,</font><font color="#800080"> {</font><font color="#000000">3</font><font color="#0000FF">,</font><font color="#000000"> 4</font><font color="#800080">}</font><font color="#0000FF">,</font><font color="#800080"> {</font><font color="#000000">5</font><font color="#0000FF">,</font><font color="#000000"> 6</font><font color="#0000FF">,</font><font color="#000000"> 7</font><font color="#800080">}</font><font color="#800000">}</font><font color="#0000FF">,</font><font color="#000000"> 2</font><font color="#0000FF">)     </font><font color="#000080"><i> -- {{2,4,6}}
</i></font><font color="#0000FF">?</font><font color="#5E005E">columnize</font><font color="#0000FF">(</font><font color="#800000">{</font><font color="#800080">{</font><font color="#000000">1</font><font color="#0000FF">,</font><font color="#000000"> 2</font><font color="#800080">}</font><font color="#0000FF">,</font><font color="#800080"> {</font><font color="#000000">3</font><font color="#0000FF">,</font><font color="#000000"> 4</font><font color="#800080">}</font><font color="#0000FF">,</font><font color="#800080"> {</font><font color="#000000">5</font><font color="#0000FF">,</font><font color="#000000"> 6</font><font color="#0000FF">,</font><font color="#000000"> 7</font><font color="#800080">}</font><font color="#800000">}</font><font color="#0000FF">,</font><font color="#800000"> {</font><font color="#000000">2</font><font color="#0000FF">,</font><font color="#000000">1</font><font color="#800000">}</font><font color="#0000FF">) </font><font color="#000080"><i> -- {{2,4,6}, {1,3,5}}</i></font>
</pre>
         </td>
        </tr>
        <tr>
         <td>
          <b>Example 2:</b>
         </td>
         <td>Use a space defval if you want strings back:
         </td>
        </tr>
        <tr>
         <td></td>
         <td>
<!--eucode>
?columnize({"abc", "def", "ghi"},defval:=' ')   -- {"adg", "beh", "cfi" }
</eucode-->
<pre>
<font color="#000000"></font><font color="#0000FF">?</font><font color="#5E005E">columnize</font><font color="#0000FF">(</font><font color="#800000">{</font><font color="#008000">"abc"</font><font color="#0000FF">,</font><font color="#008000"> "def"</font><font color="#0000FF">,</font><font color="#008000"> "ghi"</font><font color="#800000">}</font><font color="#0000FF">,</font><font color="#000000">defval</font><font color="#0000FF">:=</font><font color="#008000">' '</font><font color="#0000FF">)  </font><font color="#000080"><i> -- {"adg", "beh", "cfi" }</i></font>
</pre>
            <br>
            Otherwise you would actually get {{97'a',100'd',103'g'},{98'b',101'e',104'h'},{99'c',102'f',105'i'}} returned - 
            your program (including printf("%s") etc) should still work, however debugging/ex.err/pp()/? will be harder to decipher.
         </td>
        </tr>
        <tr>
         <td>
          <b>Example 3:</b>
         </td>
         <td>
            Declare logically related items together but generate several separate matching tables, suitable for <a href="find.htm">find</a>()
            or (perhaps/if strictly in order) <a href="binary_search.htm">binary_search</a>().
         </td>
        </tr>
        <tr>
         <td></td>
         <td>
<!--eucode>
constant {field,desc,val} = columnize({{"field1", "desc1",5},
                                       {"field2", "desc2",3}})
constant {knowntags, knowntagdescs} = columnize({
         {MI_SIGNATURE,  "Module info signature"},
         {MI_FILENAME,   "Record with full path of executable"},
         {MI_RTP,        "RTP"},
       $},{1,2})
-- which is equivalent to:
constant field = {"field1", "field2"},
         desc = {"desc1", "desc2"}
         val = {5,3}
constant knowntags = {MI_SIGNATURE,MI_FILENAME,MI_RTP},
         knowntagdescs = {"Module info signature",
                          "Record with full path of executable",
                          "RTP"}
</eucode-->
<pre>
<font color="#000000"></font><font color="#008080">constant</font><font color="#0000FF"> {</font><font color="#000000">field</font><font color="#0000FF">,</font><font color="#000000">desc</font><font color="#0000FF">,</font><font color="#000000">val</font><font color="#0000FF">} =</font><font color="#5E005E"> columnize</font><font color="#0000FF">(</font><font color="#800000">{</font><font color="#800080">{</font><font color="#008000">"field1"</font><font color="#0000FF">,</font><font color="#008000"> "desc1"</font><font color="#0000FF">,</font><font color="#000000">5</font><font color="#800080">}</font><font color="#0000FF">,
</font><font color="#800080">                                       {</font><font color="#008000">"field2"</font><font color="#0000FF">,</font><font color="#008000"> "desc2"</font><font color="#0000FF">,</font><font color="#000000">3</font><font color="#800080">}</font><font color="#800000">}</font><font color="#0000FF">)
</font><font color="#008080">constant</font><font color="#0000FF"> {</font><font color="#000000">knowntags</font><font color="#0000FF">,</font><font color="#000000"> knowntagdescs</font><font color="#0000FF">} =</font><font color="#5E005E"> columnize</font><font color="#0000FF">(</font><font color="#800000">{
</font><font color="#800080">         {</font><font color="#000000">MI_SIGNATURE</font><font color="#0000FF">, </font><font color="#008000"> "Module info signature"</font><font color="#800080">}</font><font color="#0000FF">,
</font><font color="#800080">         {</font><font color="#000000">MI_FILENAME</font><font color="#0000FF">,  </font><font color="#008000"> "Record with full path of executable"</font><font color="#800080">}</font><font color="#0000FF">,
</font><font color="#800080">         {</font><font color="#000000">MI_RTP</font><font color="#0000FF">,       </font><font color="#008000"> "RTP"</font><font color="#800080">}</font><font color="#0000FF">,
</font><font color="#000000">       $</font><font color="#800000">}</font><font color="#0000FF">,</font><font color="#800000">{</font><font color="#000000">1</font><font color="#0000FF">,</font><font color="#000000">2</font><font color="#800000">}</font><font color="#0000FF">)
</font><font color="#000080"><i>-- which is equivalent to:
</i></font><font color="#008080">constant</font><font color="#000000"> field</font><font color="#0000FF"> = {</font><font color="#008000">"field1"</font><font color="#0000FF">,</font><font color="#008000"> "field2"</font><font color="#0000FF">},
</font><font color="#000000">         desc</font><font color="#0000FF"> = {</font><font color="#008000">"desc1"</font><font color="#0000FF">,</font><font color="#008000"> "desc2"</font><font color="#0000FF">}
</font><font color="#000000">         val</font><font color="#0000FF"> = {</font><font color="#000000">5</font><font color="#0000FF">,</font><font color="#000000">3</font><font color="#0000FF">}
</font><font color="#008080">constant</font><font color="#000000"> knowntags</font><font color="#0000FF"> = {</font><font color="#000000">MI_SIGNATURE</font><font color="#0000FF">,</font><font color="#000000">MI_FILENAME</font><font color="#0000FF">,</font><font color="#000000">MI_RTP</font><font color="#0000FF">},
</font><font color="#000000">         knowntagdescs</font><font color="#0000FF"> = {</font><font color="#008000">"Module info signature"</font><font color="#0000FF">,
</font><font color="#008000">                          "Record with full path of executable"</font><font color="#0000FF">,
</font><font color="#008000">                          "RTP"</font><font color="#0000FF">}</font>
</pre>
            <br>
            This may save considerable effort when you have hundreds of fields: major chance for 
            mishap as well as eyebrainandcursorfingerache, were it not for columnize().
            <br>
            <br>
            Also note that plade.exw required (for want of a better term) nested use of columnize; instead of
<!--eucode>
constant {{iupnames,iuprids}, cem, sigs, icons, ...} = columnize({...})
</eucode-->
<pre>
<font color="#000000"></font><font color="#008080">constant</font><font color="#0000FF"> {</font><font color="#800000">{</font><font color="#000000">iupnames</font><font color="#0000FF">,</font><font color="#000000">iuprids</font><font color="#800000">}</font><font color="#0000FF">,</font><font color="#000000"> cem</font><font color="#0000FF">,</font><font color="#000000"> sigs</font><font color="#0000FF">,</font><font color="#000000"> icons</font><font color="#0000FF">, ...} =</font><font color="#5E005E"> columnize</font><font color="#0000FF">(</font><font color="#800000">{</font><font color="#0000FF">...</font><font color="#800000">}</font><font color="#0000FF">)</font>
</pre>
            <br>
            the desired 5+ matching tables were achieved through
<!--eucode>
constant {iupnamerids, cem, sigs, icons, ...} = columnize({...})
constant {iupnames,iuprids} = columnize(iupnamerids)
</eucode-->
<pre>
<font color="#000000"></font><font color="#008080">constant</font><font color="#0000FF"> {</font><font color="#000000">iupnamerids</font><font color="#0000FF">,</font><font color="#000000"> cem</font><font color="#0000FF">,</font><font color="#000000"> sigs</font><font color="#0000FF">,</font><font color="#000000"> icons</font><font color="#0000FF">, ...} =</font><font color="#5E005E"> columnize</font><font color="#0000FF">(</font><font color="#800000">{</font><font color="#0000FF">...</font><font color="#800000">}</font><font color="#0000FF">)
</font><font color="#008080">constant</font><font color="#0000FF"> {</font><font color="#000000">iupnames</font><font color="#0000FF">,</font><font color="#000000">iuprids</font><font color="#0000FF">} =</font><font color="#5E005E"> columnize</font><font color="#0000FF">(</font><font color="#000000">iupnamerids</font><font color="#0000FF">)</font>
</pre>
         </td>
        </tr>
        <tr>
         <td>
          <b>Implementation:</b>
         </td>
         <td>
            See builtins\pcolumn.e (an autoinclude) for details of the actual implementation.
         </td>
        </tr>
        <tr>
         <td>
          <b>See Also:</b>
         </td>
         <td>
          <a href="join.htm">join</a>,
          <a href="tagset.htm">tagset</a>
         </td>
        </tr>
       </table>
      </div>
     </stripped>
    </get>
   </these>
  </toc>
 </body>
</head>
