From: Robert Klemme Date: 2011-10-17T16:57:57+09:00 Subject: Re: Ruby functions to split numbers On Fri, Oct 14, 2011 at 11:54 PM, ideal one wrote: > Ian M. Asaff wrote in post #1026669: >> ruby-1.9.2-p290 :010 > a = "01234567" >>  => "01234567" >> ruby-1.9.2-p290 :011 > var1,var2 = [a[0,3],a[3,5]] >>  => ["012", "34567"] >> ruby-1.9.2-p290 :012 > var1 >>  => "012" >> ruby-1.9.2-p290 :013 > var2 >>  => "34567" >> >> keep in mind that a number with a leading zero makes ruby think the base >> is >> octal: >> >> ruby-1.9.2-p290 :019 > 012 >>  => 10 >> >> hope that helps. >             How can i auto increment the number, i tried doing > something like this... > var1 is always constant, but i want to autoincrement var2 > > value = var2 + 1  #  iam getting Cannot convert Fixnum to String Error. > is something like this works var2++, not tried this in ruby. > Preserve leading Zero and also autoincrement.. I am still not sure I get what you really want to do with those numbers. What exactly is your input (String or a Fixnum) and what do you want to do with it? Here is one more way to split and assign to local variables: irb(main):009:0> s = "1234567" => "1234567" irb(main):010:0> /\A(?\d{1,3})(?\d{5})\z/ =~ s => 0 irb(main):011:0> v1 => "12" irb(main):012:0> v2 => "34567" But if your input is already numeric other approaches are usually better, e.g. irb(main):013:0> n = 1234567 => 1234567 irb(main):014:0> v1 = n / 100000 => 12 irb(main):015:0> v2 = n % 100000 => 34567 (Assuming we're talking about decimal representation.) Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/