1

Beginner here. I've got a text area. I want to break the text up into a matrix and then transpose rows and columns. I've got function that splits the text into groups. another function that puts them into an array and that's as far as I've got. I don't really know how to combine them into one function or if that is needed. i believe i'm supposed convert the array into a matrix by creating sub arrays, then i can finally transpose the text from [ i ][ j ] to [ j ][ i ]. Is this right so far or have i left something out? I'd love to see a working example someplace but can't seem to find one.

 function G_Group(size, count)
{
   if (size <= 0)
   {
      size = document.encoder.group_size.value;
      if (size <= 0)
      {
         alert('Invalid group size');
     return false;
      }
   }
   if (count <= 0)
   {
      count = document.encoder.group_count.value;
      if (count <= 0)
      {
         alert('Invalid group count');
     return false;
      }
   }
   var t = document.encoder.text.value;
   var o = '', groups = 0;
   t = Tr(t, " \r\n\t");
   while (t.length > 0)
   {
      if (o.length > 0)
      {
         o += ' ';
      }
      if (groups >= count)
      {
         o += "\n";
     groups = 0;
      }
      groups ++;
      o += t.slice(0, size);
      t = t.slice(size, t.length);
   }
   document.encoder.text.value = o;
   return false;
}

   function toArray()
    {
    var str = document.encoder.text.value;
    var nstr = str.split(" ");
    document.encoder.text.value = nstr;
    }
//i am having trouble with this part.  don't know how to call the document.encoder.text.value into the list and don't know how to call group_size into elementsPerSubArray 

    function toMatrix(list, elementsPerSubArray) {
        var matrix = [], i, k;
        for (i = 0, k = -1; i < list.length; i++) {
            if (i % elementsPerSubArray === 0) {
                k++;
                matrix[k] = [];
            }

            matrix[k].push(list[i]);
        }

        return matrix;
    }

Finally, i don't know how to post result back into the textarea

So far i can group the text into block of how many characters i choose from a selection box (i've got 30 options) and can make line breaks wherever i want. as far as the matrix goes, line breaks will be made after 1 group of (X) characters. Given this text:

OOMUCHABOUTMYCOUNTRYICAREALOTH

I can break it into groups like this.

OOMUC HABOU TMYCO UNTRY ICARE ALOTH

or like this if i want.

OOMUC 
HABOU 
TMYCO 
UNTRY 
ICARE 
ALOTH

What i want to do is be able to flip the matrix so it looks like this

AIUTHO
LCNMAO
...ect
HEYOUC
  • Can you show us what you've got so far? An example of the string(s) you're starting with, what you can make them into and what you want the end-result to look like? – David Thomas Nov 24 '12 at 15:09
  • okay, so i just edited the message to include some of the code. Hope this is enough. Tell me if you need anything else. function toMatrix part was found on this site. http://stackoverflow.com/questions/4492385/how-to-convert-simple-array-into-two-dimensional-arraymatrix-in-javascript-or – user1799349 Nov 24 '12 at 15:29
  • I think this can be much simpler. I'm not exactly sure what `document.encoder` is, but if you just want to map your posted input into the output, try http://jsfiddle.net/2su8s/. – pimvdb Nov 24 '12 at 16:31
  • "encoder" is the name of the textarea – user1799349 Nov 24 '12 at 18:00
  • the above text was just an example. The length of the string is different every time. So are the groupings. One of the original problem i was having is trying to get the variable length loaded initialized in the first place. The length of the rows are chosen by a selection box. I need to know how to plug in that value from the selection box. Also, it would be a great help if the code on fiddle had comments added to it. – user1799349 Nov 25 '12 at 05:06

0 Answers0