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 1 of 3 123 LastLast
Results 1 to 15 of 42
  1. #1
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    884
    Thanks
    53
    Thanked 6 Times in 5 Posts

    replacing a string within a string

    I have a long string; you can see that it is 3 segments of 35 characters, each beginning with "R":

    R1107072014092311322315424423313235R1107072014092321542132231531215535R1107072014092323333521332411242423

    What I am trying to do is replace a segment or segments according to the contents of another string:

    e.g. var bu = "02" (zero indexed indication)

    In this case, the string bu says delete the first and third segments:

    zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzR1107072014092321542132231531215535zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

    Arbitrarily, I'm choosing to wipe the indicated segments with the letter "z", so I can later wipe all the zs and be left with the middle segment only.

    This seems to work:

    Code:
    var store = readCookie('mycookie');
    // e.g. mycookie is R1107072014092311322315424423313235R1107072014092321542132231531215535R1107072014092323333521332411242423
    var bu = readCookie('mybackup');
    // e.g. mybackup is 012
    for(i=0; i < bu.length; i++) {
    var x = bu.substr(i,1);
    store = store.replace(store.substr(x*35, 35), "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz");
    }
    store = store.replace(/z+/g,'');
    alert("store = " + store);
    But I know that there is a more elegant means of achieving this without the long string of zs. My latest attempt is:

    Code:
    var store = readCookie('mycookie');
    var bu = readCookie('mybackup');
    for(i=0; i < bu.length; i++) {
    var x = bu.substr(i,1);
    for(j=0; j < 35; j++) {
    var re = new RegExp((x*35)+j), 'g');
    // alert=(re);
    var store = store.replace(re, 1), "z");
    }
    }
    store = store.replace(/z+/g,'');
    alert("store = " + store);
    It's the bit in red that stumps me. I've got myself into a bit of a muddle. Any help is appreciated.
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,989
    Thanks
    56
    Thanked 557 Times in 554 Posts
    There are a couple of if's attached to this answer...

    If the string will always have the substrings separated by the capital R and if the only possible values for "bu" are 01, 02 and 12

    then maybe this would be simpler...

    Code:
    <script>
    var str ="R1107072014092311322315424423313235R1107072014092321542132231531215535R1107072014092323333521332411242423";
    
    var obj={
    "01":3,
    "02":2,
    "12":1,
    }
    
    var bits =str.split("R");
    var bu = "02";
    var idx=obj[bu];
    
    alert("R"+bits[idx]);
    
    </script>
    then again, if you're comfortable using Array.indexOf(), you could do this:

    Code:
    <script>
    var str ="R1107072014092311322315424423313235R1107072014092321542132231531215535R1107072014092323333521332411242423";
    
    var arr=["000", "12","02","01"];
    
    var bits =str.split("R");
    var bu = "02";
    var idx=arr.indexOf(bu);
    
    alert("R"+bits[idx]);
    
    </script>

  • #3
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    884
    Thanks
    53
    Thanked 6 Times in 5 Posts
    Thank you. I will keep it for reference and study.

    Unfortunately, the segments are composed of any letters or numbers - always 35 characters in length. The number of segments can go up to 10, and the ones to wipe could be some or all of those 10.
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,989
    Thanks
    56
    Thanked 557 Times in 554 Posts
    is there any kind of pattern to it, or is it completely random? Maybe some further examples would help...

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,151
    Thanks
    80
    Thanked 4,559 Times in 4,523 Posts
    Xelawho: Read him again. When bu = "02" he means "wipe element 0 and element 2 of the array of up to 10 elements".

    So it really is dirt simple.

    Code:
    var store = "R1107072014092311322315424423313235R1107072014092321542132231531215535R1107072014092323333521332411242423R1107072014092323333521332411248811"'
    var bu = "02";
    
    var storeArr = store.substr(1).split("R"); // eliminate first R then split on the others
    
    // at this point we will have
    // storeArr = [ "1107072014092311322315424423313235",
    //              "1107072014092321542132231531215535",
    //              "1107072014092323333521332411242423",
    //              "1107072014092323333521332411248811" ];
    
    for ( var b = 0; b < bu.length; ++b )
    {
        var which = Number( bu.charAt(b) );
        if ( which < storeArr.length )
        {
            storeArr[ bu[b] ] = ""; // zap the contents of the given array element, if in range
        }
    }
    
    store = storeArr.join("R");
    // at this point, store will contain
    //     1107072014092321542132231531215535RR1107072014092323333521332411248811
    store = "R" + store.replace("RR","R"); // and this fixes it up!
    //     R1107072014092321542132231531215535R1107072014092323333521332411248811
    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
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    884
    Thanks
    53
    Thanked 6 Times in 5 Posts
    There's a logic to it, but I'm not certain it will help shorten anything:

    R1107072014092311322315424423313235R1107072014092321542132231531215535R1107072014092323333521332411242423

    R could be R or 1, or 2, or 3, or 4, or 5.

    The blue bit is the time.
    The green bit is either a random number of 20 digits between 1 and 5 or 20 x "1"s, 20 x "2"s, 20 x "3"s, 20 x "4"s or 20 x "5"s.

    Another example:

    11107072014160222222222222222222222R110707201411325342314253425143423131107072014035511111111111111111111

    It's just the new red bit (above) of my code that doesn't work: wiping a segment with "zs" - but not using that ugly string of zs to do it. It doesn't seem a very professional way to achieve the wipe.
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

  • #7
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    884
    Thanks
    53
    Thanked 6 Times in 5 Posts
    That's a neat way of thinking about it. Trouble is, because the first character of a segment is not always "R", I have to resort to counting in 35s.

    (Sorry - I seem to have misled in my first post by implying the first character was always "R". These things are sometimes difficult to express when trying to simplify.)
    Last edited by tpeck; 08-07-2014 at 04:53 AM.
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

  • #8
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    947
    Thanks
    0
    Thanked 130 Times in 129 Posts
    How about:
    Code:
    store="R1107072014092311322315424423313235R1107072014092321542132231531215535R1107072014092323333521332411242423";
    var bu="02"
    segments=[];
    while (store.length>=35){
    segments.push(store.substring(0,35));
    store=store.substring(35);
    }
    for (D=0;D<segments.length;D++){
    if (bu.indexOf(D)!=-1){segments[D]='';}
    }
    store=segments.join('');

  • Users who have thanked Lerura for this post:

    tpeck (08-07-2014)

  • #9
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    884
    Thanks
    53
    Thanked 6 Times in 5 Posts
    Lerura, that works if the var bu is "02", but if it is, say, var bu="024", it doesn't wipe all three segments:

    I tried with another long string possibility of 4 segments: store="11107072014131411111111111111111111R1107072014131421515544455315341222R1107072014131644544345 224312355451R1107072014131624125113225215151434";

    EDIT: AAAARRRRGGGGHHHHH!!!!! I messed up. It can't be "024" with 4 segments. I momentarily forgot the zero indexing.

    Lerura, it works. Thanks!
    Last edited by tpeck; 08-07-2014 at 05:45 AM.
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,151
    Thanks
    80
    Thanked 4,559 Times in 4,523 Posts
    Can be done a little bit more efficiently.

    Code:
    store="R1107072014092311322315424423313235R1107072014092321542132231531215535R1107072014092323333521332411242423";
    var bu="02"
    segments=[];
    while (store.length>=35)
    {
        segments.push(store.slice(0,35));
    }
    for ( b = 0; b < bu.length; ++b )
    {
        if ( Number(bu[b]) < segments.length ) segments[ Number(bu[b]) ] = "";
    }
    store=segments.join('');
    The if ( Number(bu[b]) < segments.length ) probably doesn't need to be there, as hopefully you know how many segements you have in the cookie.
    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.

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,151
    Thanks
    80
    Thanked 4,559 Times in 4,523 Posts
    But you know, I think we should forget about using arrays.
    Code:
    store="R1107072014092311322315424423313235R1107072014092321542132231531215535R1107072014092323333521332411242423";
    var bu="02"
    for ( var b = bu.length-1; b >= 0; --b )
    {
        var remove = Number( bu.charAt( b ) );
        store.slice( remove * 35, remove * 35 + 35 );
    }
    By going *BACKWARDS* through the bu string, we ensure that we are indeed wiping out the right segments!
    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.

  • #12
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,989
    Thanks
    56
    Thanked 557 Times in 554 Posts
    here's my swing at it...

    Code:
    var store = "R1107072014092311322315424423313235R1107072014092321542132231531215535R1107072014092323333521332411242423";
    var bu = "02";
    var arr = store.match(/.{1,35}/g);
    for(i=0; i < bu.length+1; i++) {
    if (bu.indexOf(i)===-1){
    store =arr[i];
    	}
    }
    alert("store = " + store);
    Last edited by xelawho; 08-07-2014 at 10:37 PM.

  • #13
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    884
    Thanks
    53
    Thanked 6 Times in 5 Posts
    I'm gobsmacked at how many clever ways there are to achieve the same thing. And I thought my effort was pretty condensed!

    My next and last task is to put the store string into chronological order before I begin!

    e.g. store="12406072014131211111111111111111111R150507201309142151554445531534122241307072012131633333333 333333333333R2107072014111024125113225215151434";

    12406072014131211111111111111111111
    R1505072013091421515544455315341222
    41307072012131633333333333333333333
    R2107072014111024125113225215151434 becomes:

    41307072012131633333333333333333333
    R1505072013091421515544455315341222
    12406072014131211111111111111111111
    R2107072014111024125113225215151434


    store="41307072012131633333333333333333333R150507201309142151554445531534122212406072014131211111111 111111111111R2107072014111024125113225215151434";

    Before I start, is there an existing time comparison function to determine, say, (yr) 2013 is prior to 2014? And (hr) 0923 before 1045 for example?
    Last edited by tpeck; 08-07-2014 at 06:12 AM.
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

  • #14
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    947
    Thanks
    0
    Thanked 130 Times in 129 Posts
    Tested both of your versions, Old P.
    Though the slice return the substring of store, it does not remove it from store, resulting in recursion.

    It would be easier to sort the segments if the time was arranged as YYYYMMDDhhmm instead of DDMMYYYYhhmm.
    But it can still be done by this sortfunction:
    Code:
    function sortsegments(a,b){
        var A=a.substring(7,11)+a.substring(5,7)+a.substring(3,5)+a.substring(11,15);
        var B=b.substring(7,11)+b.substring(5,7)+b.substring(3,5)+b.substring(11,15);
      return (A>B?1:B>A?-1:0);
    }
    or if you choose to rearrange the time:
    Code:
    function sortsegments(a,b){
        var A=a.substring(3,15);
        var B=b.substring(3,15);
      return (A>B?1:B>A?-1:0);
    }
    by the way:
    if it should always end up removing all but 1 segment, you can reverse bu to contain the segment to keep:
    ending up with:
    Code:
    var store = "12406072014131211111111111111111111R150507201309142151554445531534122241307072012131633333333333333333333R2107072014111024125113225215151434";
    var bu = "1";
    var segments = store.match(/.{1,35}/g);
    segments.sort( sortsegments );
    store = segments[bu];
    alert("store = " + store);
    Last edited by Lerura; 08-07-2014 at 07:20 AM.

  • #15
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    884
    Thanks
    53
    Thanked 6 Times in 5 Posts
    That's helpful. I can see how to compare the time strings now. I could do it the other way around I suppose but, heck, they are both usable solutions. Thanks.

    <if it should always end up removing all but 1 segment>
    No, it could mean all the segments get removed.

    Just for the sake of it, this is what I am doing: the user chooses sessions from the list below, which become saved sessions and are appended to "mycookie", but they could be added non-chronologically, hence the need for a sort before invoking the routine(s) you have all been kindly assisting with.

    replacing a string within a string-last5.jpg

    The checked sessions could get displayed thus - unless sorted:

    replacing a string within a string-saved3.jpg

    After that, it's all about deleting saved sessions and (optionally) restoring them. I'm doing it client-side (cookies) instead of PHP to a DB, because I don't think the DB can or needs to manage a potentially large number of irrelevant changes.

    Sorry if this is too much information! Just thought some might like to know why I asked for help in the first place.

    P.S. I hope CF survives its changes. There has been no equal in the past. Stackoverflow gives me a headache.
    Attached Thumbnails Attached Thumbnails replacing a string within a string-last5.jpg  
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

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