SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 31 of 31
  1. #26
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    list($prefix$serial) = explode('-'$inputString);
    $outputString $prefix '-' str_pad($serial 1strlen($serial), '0'STR_PAD_LEFT); 
    I tend to think sprintf/printf is pretty unreadable and impenetrable at times, and anything where you can break it by changing the format string (ie, so it expects another parameter that you don't pass in) is bad in my book. str_pad makes it clear what your intentions are and why you are doing it, so wins in my book!

  2. #27
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    678
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    I like that one Stormrider!

  3. #28
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,087
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I like your solution too Stormrider

  4. #29
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,890
    Mentioned
    210 Post(s)
    Tagged
    12 Thread(s)
    I just realized that you can do this in Ruby:

    Code Ruby:
    serial = "xs-00001"
    new_serial = serial.next
    puts new_serial
    => "xs-00002"

    which is pretty neat when you think about it.

    It also deals with "xs-99999", outputting "xs-100000"

    Also, here's a solution in Scala (which the editor doesn't seem to have a code tag for):

    Code:
    def nextStr(s: String) = {
      val PrefixZerosDigits = """(xs-)(0*)(\d+)""".r
      val Nines = "9*".r
      
      s match {
        case PrefixZerosDigits(prefix, zs, ds) => 
          val dsInc = (ds.toInt + 1).toString
          ds match {
              case Nines() => 
                if (zs.length == 0)
                  prefix + dsInc
                else
                  prefix + zs.drop(1) + dsInc
              case _ => prefix + zs + dsInc
          }
        case _ => throw new RuntimeException("wrong format")
      }
    }
    
    println(nextStr("xs-00001"))
    => "xs-00002"

  5. #30
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,388
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by Pullo View Post
    I just realized that you can do this in Ruby:

    Code Ruby:
    serial = "xs-00001"
    new_serial = serial.next
    puts new_serial
    => "xs-00002"

    which is pretty neat when you think about it.

    It also deals with "xs-99999", outputting "xs-100000"
    Cool, I don't think it could get any simpler than that.. Ruby seems to have some nice functionality built-in.

    It's also very interesting to see the code from the more unusual languages like Scala and Haskel.

    Edit: I also thought Stormrider's use of the list function was pretty good, I don't think I've ever used it in any code but it makes for a neat solution here.

  6. #31
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    for($i = strlen($str) - 1; $i > 2; $i--)
    {
    	if($str{$i} < 9)
    	{
    		$str{$i} = $str{$i} + 1;
    		break;
    	}
    	$str{$i} = 0;
    }


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
  •