www.webdeveloper.com
Results 1 to 6 of 6

Thread: Finding info in textareas

  1. #1
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    374

    Question Finding info in textareas

    I am trying to read something like the following in a textarea
    Code:
    |Note|Dur:4th|Pos:n1
    |Note|Dur:4th|Pos:n-3
    |Note|Dur:4th|Pos:n0
    |Note|Dur:4th|Pos:#2
    |Note|Dur:4th|Pos:n3
    |Note|Dur:4th|Pos:n-2
    |Note|Dur:4th|Pos:#-2
    There are other types of records but I am only interested in the ones which start with "|Note|". The field after "Pos:" will be compared to members of:
    Code:
     var offsets = [ "n-6", "#-6", "n-5", "#-5", "n-4", "n-3", ... ]
    If there are other fields after Pos:, there will be a "|" or "!" instead of the end of the line.

    I was able to run the split("\n") command against the textarea to break it into separate lines, but I couldn't search those lines.

    I hope to add this as a new feature to ttr.htm.

    TIA

  2. #2
    Join Date
    Dec 2009
    Location
    Colorado
    Posts
    104
    Can you post the code, especially where the split is.

  3. #3
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    374
    It should be able to read the data it just produced. After "Click", "Read" tests what I am working on.

    Code:
    <html>
    <head>
    <title>Generate twelve tone row</title>
    <script language="JavaScript">
      var deck = new Array();
      var seltab = new Array();
      var keyOffset;
    function startup() {
      document.nwcform.selAll.checked = true;
      document.nwcform.bClick1.focus();
      } 
      
    function calculate(parm) {
      var offsets = [ "n-6", "#-6", "n-5", "#-5", "n-4", "n-3", "#-3", "n-2", "#-2", "n-1", "#-1", "n0",
       "n1", "#1", "n2", "#2", "n3", "n4", "#4", "n5", "#5", "n6", "#6" ];
      var i, j, OutText = "", testflags = "00000000000000000000";  // 25 of 'em.
      
      if (parm == 0) {  //Create random permutation
        keyOffset = document.nwcform.Key.selectedIndex;   
        for (i=0; i < 12; i++)
          deck[i] = i;
          
        for (j = 11; j > -1; j--) {
          var r = Math.floor(Math.random() * j); // Pick one from those remaining
          seltab[j] = deck[r];
          deck[r] = deck[j]; } // Move last of those remaining to slot just chosen 
      }
      else {  // Code to read "OutputField" to determine sequence and key goes here
        var cntNotes=0;
        var lines = new Array();
        lines = document.nwcform.OutputField.value.split("\n");
        for (i=0; lines[i]; i++) {
           var tstr = lines[i];		
    	   if (tstr.test("Note")) 
    	    for (var k = 0; k < offsets.length; k++) {  
    		  if (tstr.search(offsets[k])) {
    			seltab[cntNotes++]=k;
    			testflags[k] = 1;
                alert(testflags);			}
    	  }
    	}
    	alert(testflags);		
    		  
      }   //End of code to read OutputField
      
      OutText = "!NoteWorthyComposerClip(2.0,Single)\n";
     
      for (i=0; i < 12; i++) {
        OutText += "|Note|Dur:4th|Pos:" + offsets[seltab[i] + keyOffset] + "\n";
        deck[i] = seltab[i]; }
        
      if (document.nwcform.I1.checked) {
        OutText += "|Rest|Dur:4th|Color:0|Visibility:Default\n|Bar|Style:Single|XBarCnt:N|SysBreak:N|Color:0|Visibility:Default\n";
        OutText += '|Text|Text:"Inversion"|Font:PageSmallText|Pos:-6|Wide:N|Justify:Left|Placement:BestFit|Color:0|Visibility:Default\n';
          for (i=0; i < 12; i++) {
            seltab[i] = 11 - deck[i]; 
            OutText += "|Note|Dur:4th|Pos:" + offsets[seltab[i] + keyOffset] + "\n"; } }
    
      if (document.nwcform.R1.checked) {
        OutText += "|Rest|Dur:4th|Color:0|Visibility:Default\n|Bar|Style:Single|XBarCnt:N|SysBreak:N|Color:0|Visibility:Default\n";
        OutText += '|Text|Text:"Retrograde"|Font:PageSmallText|Pos:-6|Wide:N|Justify:Left|Placement:BestFit|Color:0|Visibility:Default\n';
          for (i=11; i > -1; i--) {
            seltab[i] = deck[i];
            OutText += "|Note|Dur:4th|Pos:" + offsets[seltab[i] + keyOffset] + "\n"; } }
           
      if (document.nwcform.IR1.checked) {
        OutText += "|Rest|Dur:4th|Color:0|Visibility:Default\n|Bar|Style:Single|XBarCnt:N|SysBreak:N|Color:0|Visibility:Default\n";
        OutText += '|Text|Text:"Invert Retro"|Font:PageSmallText|Pos:-6|Wide:N|Justify:Left|Placement:BestFit|Color:0|Visibility:Default\n';
          for (i=11; i > -1; i--) {
            seltab[i] = 11 - deck[i]; 
            OutText += "|Note|Dur:4th|Pos:" + offsets[seltab[i] + keyOffset] + "\n"; } }
           
      OutText += "!NoteWorthyComposerClip-End";
       
      document.nwcform.OutputField.value = OutText;
      
      if (document.nwcform.selAll.checked == true)
        document.nwcform.OutputField.select();
    	// New code to check out split
    	//var tmp = new Array();
    	//tmp = OutText.split("\n");
    	//for (var x = 0; tmp[x]; x++) {
    	//  alert(x + " " + tmp[x]); }
    	//End new code
       
    }
    </script>
    </head>
    
    <body onload="startup()">
    <form name="nwcform" onSubmit="calculate();return false;">
      <table width="782" height="350">
        <tr>
          <td colspan="4" width="1000" height="24">
            <b><big>Generate twelve tone row to be used in NoteWorthy Composer 2:</big></b>
          </td>
        </tr>
        <tr><td width="131" height="25">Bottom note: <select name="Key">
              <option selected="SELECTED" value="C ">C </option>
              <option value="C#">C#</option>
              <option value="D ">D </option>
              <option value="D#">D#</option>
              <option value="E ">E </option>
              <option value="F ">F </option>
              <option value="F#">F#</option>
              <option value="G ">G </option>
              <option value="G#">G#</option>
              <option value="A ">A </option>
              <option value="A#">A#</option>
              <option value="B ">B </option>
            </select></td>
            <td width="116"><input type="checkbox" name="I1" value="ON">Inversion</td>
            <td width="138"><input type="checkbox" name="R1" value="ON">Retrograde</td>
            <td width="721"><input type="checkbox" name="IR1" value="ON">Invert Retro</td>     
        <tr>
        
          <td height="27"  width="100"><input type="button" id="bClick1" value="Click" onclick="calculate(0)"></td>
          <td height="27"  width="100"><input type="button" id="bClick2" value="Read" onclick="calculate(1)"></td>
          <td width="602" height="27" colspan="3">Generated code is to be used on a treble clef. &nbsp;For other clefs make appropriate transposition.</td>
        </tr>
        <tr>
        <td colspan=5 width="1044" height="200">
          <textarea name="OutputField" rows="16" cols="120"></textarea></td></tr>
          <tr><td><input type="checkbox" name=selAll value="ON">Select All</td></tr>
      </table>
    </form>
    </body>
    </html>

  4. #4
    Join Date
    Dec 2009
    Location
    Colorado
    Posts
    104
    You have to implicitly convert OutText to a string object before you can use split.

    OutText = OutText.toString();
    var tmp = new Array();
    tmp = OutText.split("\n");

  5. #5
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    374
    The code around ten lines before <body> worked, the code after the first else was able to split out the lines (I think), but I couldn't search for Note on the split lines, much less find out where the Position was in the offset table.

    Thanks for looking.

  6. #6
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by harrierdh View Post
    You have to implicitly convert OutText to a string object before you can use split.

    OutText = OutText.toString();
    var tmp = new Array();
    tmp = OutText.split("\n");
    this is not java, relax your typing.

    1. your code is explicit, not implicit.
    2. since OutText is already a string, there's no need for toString.
    3. you don't need to pre-define arrays.

    so, all the above code is equal to:
    Code:
    var tmp = OutText.split(/\r?\n/);
    i use a RegExp instead of a string delimiter to uniformly handle mac, linux, and pc line breaks.



    wbport:

    here is a simple example of what i think you're trying to do, feel free to copy and modify.


    Code:
    //text (in lieu of textarea value for demo)
    var text="\
    |Note|Dur:4th|Pos:n1\n\
    |Note|Dur:4th|Pos:n-3\n\
    |Note|Dur:4th|Pos:n0\n\
    |Note|Dur:4th|Pos:#2\n\
    |Note|Dur:4th|Pos:n3\n\
    |Note|Dur:4th|Pos:n-2\n\
    |Note|Dur:4th|Pos:#-6";
    
    //define ok values here:
    var offsets = { "n-6":1, "#-6":1, "n-5":1, "#-5":1, "n-4":1, "n-3":1};
    
    //create a line array and output buffer:
    var ray=text.split(/\r?\n/), buff=[];
    
    //loop through lines:
    for (var it, i=0, mx=ray.length; i<mx; i++){
    	it=ray[i];
     	if(it.slice(0, 6)=="|Note|"){	it.replace(/Pos\:([^|\n\r!]+)/g,
    		function(j, a){ //test Pos:value and collect the slot # if good:
    			if(offsets[a]){ buff[buff.length]=i; }  
    		});//end replace function
    	}//end if Note
    }//next line
    
    //show any found slots, listed by line #:
    alert(buff);
    Last edited by rnd me; 02-19-2010 at 04:12 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles