javascript creating an array, that's length is the result of a match

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • stealth_spoof@yahoo.co.uk

    #1

    javascript creating an array, that's length is the result of a match

    Hi People wondering if anyone can help me with a problem I'm having

    I'm trying to create an array with an unspecified length, the length is
    based on the result i get from another task in the code that I have

    what I have so far is a page that has a text box for user input, which
    will take whatever the user enters, will count all the characters in
    the inputted text which also then uses the "split" command, to split
    the users text (string) into its individual pieces.

    the code I have so far is as follows
    <html>
    <head>

    <script type="text/javascript">
    function myfunction()
    {
    var v = document.form1. Text_Box.value;
    var chop =v;
    var x = v.length;
    var parts=chop.spli t("");
    var winPtr = window.open('', '_blank', '...features... ');
    var str = '<html>\n';
    str += '<head>\n';
    str += '<title>Dynami c Page</title>\n';
    str += '</head>\n';
    str += '<body>\n';
    str += '<p>\n';
    str += v + '\n';
    str += '<br>\n';
    str += '<br>\n';
    str += x + '\n';
    str += '<br>\n';
    str += '<br>\n';
    str += parts + '\n';
    str += '</p>\n';
    str += '</body>\n';
    str += '</html>';
    winPtr.document .open();
    winPtr.document .writeln(str);
    winPtr.document .close();
    }

    </script>

    </head>
    <body>

    <form name="form1">

    <TEXTAREA NAME="Text_Box" COLS="20" ROWS="4" VALUE=""></TEXTAREA>

    <input type="button"
    onclick="myfunc tion()"
    value="Font 1">

    </form>

    </body>
    </html>

    I'm aiming to perform some comparisons on the arrays elements, once
    I've overcome this hurdle!

    any and all help greatly appreciated!!!

    Thanks In Advance!!!

  • VK

    #2
    Re: javascript creating an array, that's length is the result of a match


    stealth_sp...@y ahoo.co.uk wrote:[color=blue]
    > Hi People wondering if anyone can help me with a problem I'm having
    >
    > I'm trying to create an array with an unspecified length, the length is
    > based on the result i get from another task in the code that I have[/color]

    var v = document.form1. Text_Box.value;

    Why this intermediary variable below? Is Text_Box a textbox or
    textarea?[color=blue]
    > var chop =v;[/color]

    var x = v.length;

    This gives you an array with a single element containing the entire
    string:[color=blue]
    > var parts=chop.spli t("");[/color]

    var parts = new Array();
    for (var i=0; i<x; x++) {
    parts[i] = v.charAt(i);
    }
    [color=blue]
    >var winPtr = window.open('', '_blank', '...features... ');
    > var str = '<html>\n';
    > str += '<head>\n';
    > str += '<title>Dynami c Page</title>\n';
    > str += '</head>\n';
    > str += '<body>\n';
    > str += '<p>\n';
    > str += v + '\n';
    > str += '<br>\n';
    > str += '<br>\n';
    > str += x + '\n';
    > str += '<br>\n';
    > str += '<br>\n';[/color]

    This now an equivalent of str += parts.join(',') + '\n';
    All array members will be joined back to one string
    with comma as separator. Of course you can use another
    separator of default one, say str += parts.join('-') + '\n';[color=blue]
    > str += parts + '\n';[/color]

    [color=blue]
    > str += '</p>\n';
    > str += '</body>\n';
    > str += '</html>';
    > winPtr.document .open();
    > winPtr.document .writeln(str);
    > winPtr.document .close();
    > }
    >
    > </script>
    >
    > </head>
    > <body>
    >
    > <form name="form1">
    >
    > <TEXTAREA NAME="Text_Box" COLS="20" ROWS="4" VALUE=""></TEXTAREA>
    >
    > <input type="button"
    > onclick="myfunc tion()"
    > value="Font 1">
    >
    > </form>
    >
    > </body>
    > </html>
    >
    > I'm aiming to perform some comparisons on the arrays elements, once
    > I've overcome this hurdle!
    >
    > any and all help greatly appreciated!!!
    >
    > Thanks In Advance!!![/color]

    Comment

    • RobG

      #3
      Re: javascript creating an array, that's length is the result ofa match

      stealth_spoof@y ahoo.co.uk wrote:[color=blue]
      > Hi People wondering if anyone can help me with a problem I'm having
      >
      > I'm trying to create an array with an unspecified length, the length is
      > based on the result i get from another task in the code that I have
      >
      > what I have so far is a page that has a text box for user input, which
      > will take whatever the user enters, will count all the characters in
      > the inputted text which also then uses the "split" command, to split
      > the users text (string) into its individual pieces.
      >
      > the code I have so far is as follows
      > <html>
      > <head>
      >
      > <script type="text/javascript">
      > function myfunction()
      > {
      > var v = document.form1. Text_Box.value;
      > var chop =v;
      > var x = v.length;
      > var parts=chop.spli t("");[/color]

      'parts' is now an array of the individual characters of 'chop'.
      Incidentally, the length of 'parts' will be the same as v.length, i.e.
      the value you assigned to 'x'. Now it is available in 3 places, x seem
      redundant.

      [color=blue]
      > var winPtr = window.open('', '_blank', '...features... ');
      > var str = '<html>\n';[/color]

      When writing a document, you should make sure you write valid HTML,
      including a doctype. You can omit the head and body tags if you like,
      they are optional.

      [color=blue]
      > str += '<head>\n';
      > str += '<title>Dynami c Page</title>\n';
      > str += '</head>\n';
      > str += '<body>\n';
      > str += '<p>\n';
      > str += v + '\n';
      > str += '<br>\n';
      > str += '<br>\n';
      > str += x + '\n';
      > str += '<br>\n';
      > str += '<br>\n';
      > str += parts + '\n';
      > str += '</p>\n';
      > str += '</body>\n';
      > str += '</html>';[/color]

      String concatenation using the '+=' compound operator is notoriously
      slow in some browsers, though you won't notice it here. You could put
      all this in an array:

      var str = [
      '<html>',
      '<head>',
      '<title>Dynami c Page<\/title>',
      ...
      '<\/body>',
      '<\/html>'
      ];


      Note all the newlines are removed (less to type :-) ). Then write it with:

      winPtr.document .write(str.join ('\n'));

      Adding newlines will just make the HTML look better when you use view
      source, otherwise they aren't necessary.

      A joined array may not be faster in all browsers, but it is in most. If
      nothing else, it is less to type. The forward-slash character should be
      quoted when it follows '<' when using document.write.

      [color=blue]
      > winPtr.document .open();[/color]

      There is no need to call document.open() , it is called automatically
      with document.write( ).

      [color=blue]
      > winPtr.document .writeln(str);
      > winPtr.document .close();
      > }
      >
      > </script>
      >
      > </head>
      > <body>
      >
      > <form name="form1">[/color]

      Form elements require an action attribute, even if it's empty.

      [color=blue]
      > <TEXTAREA NAME="Text_Box" COLS="20" ROWS="4" VALUE=""></TEXTAREA>[/color]

      There is no HTML value attribute for a textarea, its value is its text
      content.

      <URL:http://www.w3.org/TR/html4/interact/forms.html#edef-TEXTAREA>

      [color=blue]
      >
      > <input type="button"
      > onclick="myfunc tion()"
      > value="Font 1">
      >
      > </form>
      >
      > </body>
      > </html>
      >
      > I'm aiming to perform some comparisons on the arrays elements, once
      > I've overcome this hurdle![/color]

      What hurdle? What are you trying to do? There is almost never any need
      to declare the length of a JavaScript array, it is self-adjusting. Just
      insert elements wherever you want:

      // Initialise an array named 'A'
      var A = [];

      // Give the 6th element a value
      A[5] = 'blah';


      After step 1, A has a length of 0. After adding an element at index 5,
      it has a length of 6 - the length property of an array is special, it is
      always larger than the highest index that has been given a value and it
      adjusts automatically. Read the ECMAScript Language specification
      Section 15.4, particularly section 15.4.4 Properties of the Array
      Prototype Object, it is quite readable.

      JavaScript arrays are also sparse, that is, they only take up memory for
      the elements that have been given a value. Search the archives, there
      have been some long and detailed threads on arrays within the last 6 months.



      --
      Rob

      Comment

      Working...