www.webdeveloper.com
Results 1 to 6 of 6

Thread: Finding info in textareas

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

    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
    585
    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
    585
    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.
    Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.15105 seconds
  • Memory Usage 2,913KB
  • Queries Executed 15 (?)
More Information
Template Usage (34):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (5)bbcode_code
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (6)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (6)postbit
  • (6)postbit_onlinestatus
  • (6)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (71):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates