Page 2 of 2 FirstFirst 12
Results 11 to 19 of 19

Thread: Parse a string like "1-5,10,21,25-30"

  1. #11
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    Code:
    function parseSet(set) {
        var result = [], lastIndex = 0, list = set.split(',');
        for (var i = 0, len = list.length; i < len; i++) {
                var range = list[i].split('-');
                if (range.length == 1) result[lastIndex++] = range[0] - 0;
                else for (var num = range[0] - 0, max = range[1] - 0; num <= max; num++) result[lastIndex++] = num;
        }
        return result;
    }
    
    parseSet("1,2,3,17,81,8-17,11,12,21-23,99,100-103");
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

  2. #12
    Join Date
    Aug 2002
    Posts
    6,223
    Rep Power
    0

    Default

    icymint3, are you sure that code is in VB? That looks more like C.....
    .
    PC - Ubuntu 15.04 64bit Desktop
    HP Pav G60-236US 3GB RAM Laptop, Ubuntu 15.04 64bit and Win7 Home

    "So Daddy, how come you telling me stealing not right when YOU copying DVDs? How come? How Come?"


    RIP Ramesh ...

  3. #13
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    sorry, that was javascript... i just did it in my browser, couldnt bother fire up my IDE to do VB, im not a fan of VB either
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

  4. #14
    girldemsuga Guest

    Default

    Your code is less but you didn't check for duplications.
    When I was constructing the function, I could have done that part in another function but I wanted this function to be self reliant.

  5. #15
    digimon Guest

    Default

    Unuh idle eh?

    .

  6. #16
    girldemsuga Guest

    Default

    What is so idle about what being discussed (not attacking, just asking)?

  7. #17
    Join Date
    Jan 2009
    Posts
    2,404
    Rep Power
    0

    Default

    Quote Originally Posted by girldemsuga View Post
    I run many error checks and ensure the same value is not inserted more than once.
    That may account for the larger code.

    I am interested in seeing what your version of this looks like.
    Code:
    $chapters = StringSplit( $chapters, "," )
    
    For $inc = 1 To $chapters[ 0 ]
    	$curChap = $chapters[ $inc ]
    	
    	If StringInStr( $curChap, "-" ) Then
    		$curChap = StringSplit( $curChap, "-" )
    		
    		For $inc2 = $curChap[ 1 ] To $curChap[ 2 ]
    			FileWriteLine( $dlLog, "Grabbing chapter " & $inc2 )
    			GetChap( $inc2, $firstPage )
    		Next
    		
    	Else
    		FileWriteLine( $dlLog, "Grabbing chapter " & $curChap )
    		GetChap( $curChap, $firstPage )
    	EndIf
    Next
    Felt lazy, so I just ripped the relevant code from my AutoIt3 script and removed a few comments and unrelated statements. All you'd need to do is zap the GetChap function and use it's first argument to build your array. 12 lines , easy to convert to VB since they're both BASIC descendants.

    Quote Originally Posted by icymint3 View Post
    Code:
    function parseSet(set) {
        var result = [], lastIndex = 0, list = set.split(',');
        for (var i = 0, len = list.length; i < len; i++) {
                var range = list[i].split('-');
                if (range.length == 1) result[lastIndex++] = range[0] - 0;
                else for (var num = range[0] - 0, max = range[1] - 0; num <= max; num++) result[lastIndex++] = num;
        }
        return result;
    }
    
    parseSet("1,2,3,17,81,8-17,11,12,21-23,99,100-103");
    icymint3, you're cheating . Not because it's a free-form language, the code should be easily readable. You should've broken those into logical lines (giving you 13+ lines )...

    Quote Originally Posted by digimon View Post
    Unuh idle eh?

    .
    Are you saying that programming is idling? (I for one am ready to attack...)
    Rooted OnePlus 2 64GB Ed, Android 5.1.1 OxygenOS ; on teifin' AT&T's network; Rooted ASUS Transformer TF101 w/ dock, Android 5.1 KatKiss; Laptop: ASUS X550C, 2.0GHzx2, 8GB, 512GB SSD, Kubuntu 15.10;
    Facebook page: Skeleville Technology Solutions

  8. #18
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    Code:
    void main(){ 
      char set[] = "1,3,5-7,9,11,23-27,30", currChar, *lowerRange = set, *upperRange = set;
      int low, hi, i = 0;  
      do{
        currChar = set[i]; // get the char before we destroy the original string
        if(currChar == '-'){        // check if we need a pointer to second number
          upperRange = &set[i] + 1; // point upper to next char
          set[i] = 0;               // end the lower range string 
        }
        if(currChar == ',' || set[i + 1] == 0){  // check for comma or end of string
          if(currChar == ',') set[i] = 0;        // end the 'number string'
          for(low = atoi(lowerRange), hi = atoi(upperRange); low <= hi; low++)
            printf("%d, ", low);
          lowerRange = upperRange = &set[i] + 1; // point lower/upper to next char
        }
      } while(set[++i] != 0);
    }
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

  9. #19
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    C# ... this would be shorter with Linq
    Code:
    public static void RunSnippet()
    {
    	string set = "1,3,5-7,9,11,23-27,30";
    	string[] range;
    	int low, hi;
    	
    	foreach(string entry in set.Split(','))
    		for(range = entry.Split('-'), low = int.Parse(range[0]), hi = int.Parse(range[range.Length -1]); low <= hi ; low++)
    				W("{0}, ", low); 
    }
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

Posting Permissions

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