Professional Web Applications Themes

how to find 500th space char in string? - Coldfusion - Advanced Techniques

I am working on a listserv at the moment, and part of the requirement is that the system restricts messages to an admin specifiable number of words. The idea is that as the messages come in, the system will count to eg: 500 spaces, then dump the rest. I'm aware this is not wholly accurate, not taking into account multiple spaces or tabs, linefeeds etc, but this is all that is needed at present. If anyone can shed some light on this it would be very helpful - I've tried treating the message as a list delimited by spaces (which ...

  1. #1

    Default how to find 500th space char in string?

    I am working on a listserv at the moment, and part of the requirement is that
    the system restricts messages to an admin specifiable number of words. The idea
    is that as the messages come in, the system will count to eg: 500 spaces, then
    dump the rest. I'm aware this is not wholly accurate, not taking into account
    multiple spaces or tabs, linefeeds etc, but this is all that is needed at
    present.

    If anyone can shed some light on this it would be very helpful - I've tried
    treating the message as a list delimited by spaces (which obviously sorts out
    the problem of multiple spaces) - but looping through the list to delete
    everyhting after the 500th element takes way to long. a list based equivalent
    of Left() would be ideal, but saddly lacking. If i could just convert the
    location of listgetat(listname, 500, " ") into the actual string location i'd
    be set!

    Cheers,
    Aegis

    A3gis Guest

  2. #2

    Default Re: how to find 500th space char in string?

    So exactly how long does it take to do this anyhow.
    <cfscript>
    x = "";
    for (j = 1; j lte 500; j = j +1)
    x=x & listgetat(thelist, j, " ");
    <cfscript>
    Couldn't be more than a second.


    Dan Guest

  3. #3

    Default Re: how to find 500th space char in string?

    You might want to make sure that space stays in there, though, and the words just don't get jumbled together...

    x=x & listgetat(thelist, j, " ") & " ";

    Good stuff, though, Dan.
    Kronin555 Guest

  4. #4

    Default Re: how to find 500th space char in string?

    Best if you can cut down the number of times it actually needs to do anything.
    In other words, if the word count is less than 500 then you don't have to do
    anything - no point rebuilding a string if you don't need to.

    <cfloop condition="ListLen(stringVar, ' ') gt 500">
    <cfset stringVar = ListDeleteAt(stringVar, 501, ' ')>
    </cfloop>

    cheers.


    efecto747 Guest

  5. #5

    Default Re: how to find 500th space char in string?

    Thanks Dan and Kronin555, that's done the trick - I had no idea cfscript was so
    much faster than looping through a list!

    thanks also efecto, I had tried a list loop initially, but it was taking
    forever (especially with the possibility of the word limit going as high as
    5,000 words) - I did have the if statement in checking against the word limit
    specified for the list though! but good to mention as so many people dont think
    to do that! **looks over at work mates pointedly**

    Thanks All!
    Aegis

    A3gis Guest

  6. #6

    Default Re: how to find 500th space char in string?

    Ok, I've been doing some more thinking about this. A better option would be to
    insert a unique marker (something that won't appear in the text) at the point
    you want to delete from so you can easily locate the point with a Find()
    function

    The following should do the trick (not tested):

    <cfif ListLen(stringVar, " ") gt 500>
    <cfset stringVar = ListInsertAt(stringVar, 501, "DELETE_FROM_HERE", " ")>
    <cfset stringVar = Left(stringVar, Find("DELETE_FROM_HERE", stringVar))>
    </cfif>

    cheers.

    efecto747 Guest

  7. #7

    Default Re: how to find 500th space char in string?

    actually that is a good point... I was thinking about that with delimiters and
    listfind, which of course only delimits on one character.. completly forgot
    that i could add a string of grarbage in and delete from the start of that
    using find+left might check the speed difference and let you know which one
    runs best!

    Cheers,
    Aegis

    A3gis Guest

  8. #8

    Default Re: how to find 500th space char in string?

    woo... results are in!
    <cfscript> one takes 4562ms to complete
    listInsertAt variant takes 5922ms to complete

    (takes so long because it's running once for the text msg and again for the
    html msg)
    email sent through was 10,000 words long (just because .. )

    Thanks everyone for your help with this :)
    Aegis

    A3gis Guest

  9. Moderated Post

    Default Re: how to find 500th space char in string?

    Removed by Administrator
    jamest827 Guest
    Moderated Post

Similar Threads

  1. #39204 [NEW]: MsSq_Fetch_* return Empty (Var)Char as single space
    By bertrand dot lepoittevin at groupe-mma dot fr in forum PHP Bugs
    Replies: 2
    Last Post: October 20th, 08:59 AM
  2. #26315 [Com]: mssql_fectch_* functions are returning a space char on empty fields
    By w1lz0r at barrysworld dot net in forum PHP Development
    Replies: 3
    Last Post: February 26th, 06:54 AM
  3. Replies: 0
    Last Post: November 19th, 02:44 PM
  4. #26315 [NEW]: mssql_fectch_* functions are returning a space char on empty fields
    By webmaster at cbre dot fr in forum PHP Development
    Replies: 0
    Last Post: November 19th, 12:22 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139