Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 2 FirstFirst 12
Results 16 to 23 of 23
  1. #16
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Well, it's pretty inefficient to have to keep invoking document.getElementById("xxx").value all the time.

    Better would be to do
    Code:
        var what = document.getElementById("xxx").value;
    at the top of the function so you only do that code once.

    But realistically, the I/O to the file is going to be eating up so much time that a minor optimization like that likely won't matter.

    By the by: If you'd like to ignore the case (upper/lower) when you do the indexOf( ) then a better way would be to construct a regular expression.

    Something iike:
    Code:
        var what = new RegExp( document.getElementById("xxx").value, "gi" );
    and then replace the indexOf with
    Code:
        if ( what.test( lines[n] ) ...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  2. #17
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by Old Pedant View Post
    By the by: If you'd like to ignore the case (upper/lower) when you do the indexOf( ) then a better way would be to construct a regular expression.

    Something iike:
    Code:
        var what = new RegExp( document.getElementById("xxx").value, "gi" );
    and then replace the indexOf with
    Code:
        if ( what.test( lines[n] ) ...
    but what if they type 9-11-2001, $19.95, or even 1.5 into #xxx ?
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  3. #18
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Quote Originally Posted by rnd me View Post
    but what if they type 9-11-2001, $19.95, or even 1.5 into #xxx ?
    Good point. Okay, so just use good old .toLowerCase() on both "what" and the lines[n].

    (But why wouldn't 9-11-2001 still work? Not that it matters. Clearly the period in the others is a killer, not to mention many other possible characters.)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  4. #19
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by Old Pedant View Post
    Good point. Okay, so just use good old .toLowerCase() on both "what" and the lines[n].

    (But why wouldn't 9-11-2001 still work? Not that it matters. Clearly the period in the others is a killer, not to mention many other possible characters.)
    i guess it would, outside of brackets, good eye.

    you can do:
    Code:
      var what = new RegExp( document.getElementById("xxx").value.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1'), "gi" );
    with any input, afaik...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  5. #20
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    DOH. Yeah, I should have thought of that.

    But what the heck, you could just as easily do:
    Code:
      var what = new RegExp( document.getElementById("xxx").value.replace(/(\W)/g, '\\$1'), "gi" );
    No?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  6. #21
    New Coder
    Join Date
    Mar 2012
    Posts
    78
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi again,

    I came across a slight problem.

    The code works fine at deleting the line a word is in but it is not specific to the word entered such as "number" because another line in the example set from the original post has the word "numbered" and that gets deleted because it also contains the shorter "number" word.

    Any thoughts on a way to make the one word "number" a string or with a limit boundary at start and end to only remove the line with the exact match to actual word used.

    I tried this..... ("/\\b +what+ \\b/i") as seen in code below but know I have it wrong in some way.


    I know you are a lot smarter than I but I have to go with what works for me even if it is not a perfect code in your view. I stuck with the indexOf and used the var 'what' for the get element by id part as overall the code done that way works and I can follow it in my limited way.

    The added boundary to variable 'what' of course fails to work the way I have it and I need some help setting that up correctly.

    Code:
    <script type="text/javascript">
    function removeLineContaining()
    {
        var what = document.getElementById("xxx").value.toLowerCase();
        var fso = new ActiveXObject("Scripting.FileSystemObject");
        var infile = fso.OpenTextFile("c:\\path\\to\\file.txt");
        var lines = infile.ReadAll().split("\n");
        var removed = [ ];
        infile.Close( );
        for ( var n = lines.length-1; n >= 0; --n )
        {
            if ( lines[n].toLowerCase().indexOf("/\\b "+what+" \\b/i") >= 0 )
            {
                removed.push( lines[n].toLowerCase() );
                lines.splice( n, 1 );
            }
        }
        var outfile = fso.CreateTextFile("c:\\path\\to\\file.txt", true); // true==overwrite
        outfile.write( lines.join("\n") );
        outfile.Close();
        return removed;
    }
    </script>

    Martin.

  7. #22
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    indexOf() can't use a regular expression.

    You would need to adapt the regexp code we showed you:
    Code:
        // first "escape" all except \w characters...
        // that is, convert "ab-12" to "ab\-12"
        var what = document.getElementById("xxx").value.replace(/(\W)/g, "\\$1");
        // then use that to create a regexp looking only for *WORDS*:
        what = new Regexp("\\b" + what + "\\b", "gi" );
        ...
        for ( var n = lines.length-1; n >= 0; --n )
        {
            if ( what.test( lines[n] ) )
            {
                removed.push( lines[n].toLowerCase() );
                lines.splice( n, 1 );
            }
        }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  8. #23
    New Coder
    Join Date
    Mar 2012
    Posts
    78
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi to all who are reading this thread.

    If you have used the various suggestions and failed to get some working, take a look at the spelling of RegExp as I have on a few occasions kept putting Regexp without an upper case E and things just fail.


    Martin.


 
Page 2 of 2 FirstFirst 12

Posting Permissions

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