KathysKode-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Jul-13 13:01 UTC
Get the first character from a string?
I''ve got a math function where I''m converting a base36 number to a base 10 integer and it works wonderfully, except when the first character is a zero. I could get this method to work if I could look at the first position in a string, and if it was a zero, I''d then add a zero to the first position of the resulting string. Here''s the equation (and its compliment); def calc_ovalue(barcode) barcode.to_i(36) end def calc_other(barcode) onum = calc_ovalue(barcode) if onum.odd? (onum + 1).to_s(36).upcase else (onum - 1).to_s(36).upcase end end some examples of accepeting the first value and calculating the second two are; 0N5F 0N5G ovalue = 30003 0V9B 0V9C ovalue = 40511 6JYV 6JYW ovalue = 305815 Thank you, Kathleen --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
if you have a string @str, then @str[0] will give you the charactercode of the first char that''s already enough to test against the charcode of a zero which is 48 if yo prefer to work with a string as result, use @str[0,1] this gives a string result (0 = from position, 1 = length) or @str[0..0] returns a string too, the range is startposition to endposition --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Assuming that s is the string; s[0] is the first character. s[0].chr ="1" or s[0]==48 (see the irb session below)>> a="1234"=> "1234">> b="01234"=> "01234">> a[0]=> 49>> a[0].chr=> "1">> b[0]=> 48>> b[0].chr=> "0">>Hope that helps, Matt On Sun, 2008-07-13 at 06:01 -0700, KathysKode-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> I''ve got a math function where I''m converting a base36 number to a > base 10 integer and it works wonderfully, except when the first > character is a zero. > I could get this method to work if I could look at the first position > in a string, and if it was a zero, I''d then add a zero to the first > position of the resulting string. > Here''s the equation (and its compliment); > > def calc_ovalue(barcode) > barcode.to_i(36) > end > > def calc_other(barcode) > onum = calc_ovalue(barcode) > if onum.odd? > (onum + 1).to_s(36).upcase > else > (onum - 1).to_s(36).upcase > end > end > > some examples of accepeting the first value and calculating the second > two are; > 0N5F 0N5G ovalue = 30003 > 0V9B 0V9C ovalue = 40511 > 6JYV 6JYW ovalue = 305815 > > Thank you, > Kathleen > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
KathysKode-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Jul-13 18:06 UTC
Re: Get the first character from a string?
Matt, I''m seeing how to test the first character of the incoming string which is (barcode) (see function) def calc_other(barcode) onum = calc_ovalue(barcode) if onum.odd? (onum + 1).to_s(36).upcase else (onum - 1).to_s(36).upcase end if barcode[0] == 48 "0" + return end end But then, I don''t see how to attach the preceding zero if the incoming (barcode) has one. I know this looks stupid, but what I''m attempting is to add a zero to the first position of the result. I assume result = string? Thank you, Kathleen On Jul 13, 8:32 am, Matt Williams <m...-djWhm22DhGpWk0Htik3J/w@public.gmane.org> wrote:> Assuming that s is the string; s[0] is the first character. s[0].chr => "1" or s[0]==48 (see the irb session below) > > >> a="1234" > => "1234" > >> b="01234" > => "01234" > >> a[0] > => 49 > >> a[0].chr > => "1" > >> b[0] > => 48 > >> b[0].chr > => "0" > > Hope that helps, > Matt > > On Sun, 2008-07-13 at 06:01 -0700, KathysK...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: > > I''ve got a math function where I''m converting a base36 number to a > > base 10 integer and it works wonderfully, except when the first > > character is a zero. > > I could get this method to work if I could look at the first position > > in a string, and if it was a zero, I''d then add a zero to the first > > position of the resulting string. > > Here''s the equation (and its compliment); > > > def calc_ovalue(barcode) > > barcode.to_i(36) > > end > > > def calc_other(barcode) > > onum = calc_ovalue(barcode) > > if onum.odd? > > (onum + 1).to_s(36).upcase > > else > > (onum - 1).to_s(36).upcase > > end > > end > > > some examples of accepeting the first value and calculating the second > > two are; > > 0N5F 0N5G ovalue = 30003 > > 0V9B 0V9C ovalue = 40511 > > 6JYV 6JYW ovalue = 305815 > > > Thank you, > > Kathleen--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
will the result always be 4 chars? If so, could you do something like return_val = my_val.rjust(4).gsub(" ","0") On Sun, Jul 13, 2008 at 1:06 PM, KathysKode@gmail.com <KathysKode@gmail.com> wrote:> > Matt, > I'm seeing how to test the first character of the incoming string > which is (barcode) (see function) > def calc_other(barcode) > onum = calc_ovalue(barcode) > if onum.odd? > (onum + 1).to_s(36).upcase > else > (onum - 1).to_s(36).upcase > end > if barcode[0] == 48 > "0" + return > end > end > > But then, I don't see how to attach the preceding zero if the incoming > (barcode) has one. I know this looks stupid, but what I'm attempting > is to add a zero to the first position of the result. I assume result > = string? > Thank you, > Kathleen > > On Jul 13, 8:32 am, Matt Williams <m...@harpstar.com> wrote: > > Assuming that s is the string; s[0] is the first character. s[0].chr => > "1" or s[0]==48 (see the irb session below) > > > > >> a="1234" > > => "1234" > > >> b="01234" > > => "01234" > > >> a[0] > > => 49 > > >> a[0].chr > > => "1" > > >> b[0] > > => 48 > > >> b[0].chr > > => "0" > > > > Hope that helps, > > Matt > > > > On Sun, 2008-07-13 at 06:01 -0700, KathysK...@gmail.com wrote: > > > I've got a math function where I'm converting a base36 number to a > > > base 10 integer and it works wonderfully, except when the first > > > character is a zero. > > > I could get this method to work if I could look at the first position > > > in a string, and if it was a zero, I'd then add a zero to the first > > > position of the resulting string. > > > Here's the equation (and its compliment); > > > > > def calc_ovalue(barcode) > > > barcode.to_i(36) > > > end > > > > > def calc_other(barcode) > > > onum = calc_ovalue(barcode) > > > if onum.odd? > > > (onum + 1).to_s(36).upcase > > > else > > > (onum - 1).to_s(36).upcase > > > end > > > end > > > > > some examples of accepeting the first value and calculating the second > > > two are; > > > 0N5F 0N5G ovalue = 30003 > > > 0V9B 0V9C ovalue = 40511 > > > 6JYV 6JYW ovalue = 305815 > > > > > Thank you, > > > Kathleen > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
KathysKode-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Jul-13 20:22 UTC
Re: Get the first character from a string?
Marlon, Yes. The problem is that if I submit a barcode like 0123 the other barcode is calculated as 124. If there is a character other than zero in the first position it works just fine. Where are you suggesting that I put the return_val line of code you show? Is that gsub example putting a zero at the end of the string? What / How would you amend the return value of the calc_other function. Isn''t the result of any method the value inside the ''return'' keyword? If so, why can''t the ''return'' value be modified before the function closes? I seem to be having a problem ''seeing the forest through the trees''. Kathleen On Jul 13, 1:20 pm, "Marlon Moyer" <marlon.mo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> will the result always be 4 chars? If so, could you do something like > > return_val = my_val.rjust(4).gsub(" ","0") > > On Sun, Jul 13, 2008 at 1:06 PM, KathysK...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <KathysK...-Re5JQEeQqe8@public.gmane.orgm> > wrote: > > > > > Matt, > > I''m seeing how to test the first character of the incoming string > > which is (barcode) (see function) > > def calc_other(barcode) > > onum = calc_ovalue(barcode) > > if onum.odd? > > (onum + 1).to_s(36).upcase > > else > > (onum - 1).to_s(36).upcase > > end > > if barcode[0] == 48 > > "0" + return > > end > > end > > > But then, I don''t see how to attach the preceding zero if the incoming > > (barcode) has one. I know this looks stupid, but what I''m attempting > > is to add a zero to the first position of the result. I assume result > > = string? > > Thank you, > > Kathleen > > > On Jul 13, 8:32 am, Matt Williams <m...-djWhm22DhGpWk0Htik3J/w@public.gmane.org> wrote: > > > Assuming that s is the string; s[0] is the first character. s[0].chr => > > "1" or s[0]==48 (see the irb session below) > > > > >> a="1234" > > > => "1234" > > > >> b="01234" > > > => "01234" > > > >> a[0] > > > => 49 > > > >> a[0].chr > > > => "1" > > > >> b[0] > > > => 48 > > > >> b[0].chr > > > => "0" > > > > Hope that helps, > > > Matt > > > > On Sun, 2008-07-13 at 06:01 -0700, KathysK...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: > > > > I''ve got a math function where I''m converting a base36 number to a > > > > base 10 integer and it works wonderfully, except when the first > > > > character is a zero. > > > > I could get this method to work if I could look at the first position > > > > in a string, and if it was a zero, I''d then add a zero to the first > > > > position of the resulting string. > > > > Here''s the equation (and its compliment); > > > > > def calc_ovalue(barcode) > > > > barcode.to_i(36) > > > > end > > > > > def calc_other(barcode) > > > > onum = calc_ovalue(barcode) > > > > if onum.odd? > > > > (onum + 1).to_s(36).upcase > > > > else > > > > (onum - 1).to_s(36).upcase > > > > end > > > > end > > > > > some examples of accepeting the first value and calculating the second > > > > two are; > > > > 0N5F 0N5G ovalue = 30003 > > > > 0V9B 0V9C ovalue = 40511 > > > > 6JYV 6JYW ovalue = 305815 > > > > > Thank you, > > > > Kathleen--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
This is how it could be done: def calc_ovalue(barcode) barcode.to_i(36) end def calc_other(barcode) onum = calc_ovalue(barcode) if onum % 2 == 1 onum+=1 else onum-=1 end onum.to_s(36).rjust(4).gsub(" ","0").upcase end On Sun, Jul 13, 2008 at 3:22 PM, KathysKode@gmail.com <KathysKode@gmail.com> wrote:> > Marlon, > Yes. The problem is that if I submit a barcode like 0123 the other > barcode is calculated as 124. If there is a character other than zero > in the first position it works just fine. > Where are you suggesting that I put the return_val line of code you > show? > Is that gsub example putting a zero at the end of the string? > What / How would you amend the return value of the calc_other > function. Isn't the result of any method the value inside the 'return' > keyword? > If so, why can't the 'return' value be modified before the function > closes? > I seem to be having a problem 'seeing the forest through the trees'. > Kathleen > > On Jul 13, 1:20 pm, "Marlon Moyer" <marlon.mo...@gmail.com> wrote: > > will the result always be 4 chars? If so, could you do something like > > > > return_val = my_val.rjust(4).gsub(" ","0") > > > > On Sun, Jul 13, 2008 at 1:06 PM, KathysK...@gmail.com < > KathysK...@gmail.com> > > wrote: > > > > > > > > > Matt, > > > I'm seeing how to test the first character of the incoming string > > > which is (barcode) (see function) > > > def calc_other(barcode) > > > onum = calc_ovalue(barcode) > > > if onum.odd? > > > (onum + 1).to_s(36).upcase > > > else > > > (onum - 1).to_s(36).upcase > > > end > > > if barcode[0] == 48 > > > "0" + return > > > end > > > end > > > > > But then, I don't see how to attach the preceding zero if the incoming > > > (barcode) has one. I know this looks stupid, but what I'm attempting > > > is to add a zero to the first position of the result. I assume result > > > = string? > > > Thank you, > > > Kathleen > > > > > On Jul 13, 8:32 am, Matt Williams <m...@harpstar.com> wrote: > > > > Assuming that s is the string; s[0] is the first character. s[0].chr > => > > > "1" or s[0]==48 (see the irb session below) > > > > > > >> a="1234" > > > > => "1234" > > > > >> b="01234" > > > > => "01234" > > > > >> a[0] > > > > => 49 > > > > >> a[0].chr > > > > => "1" > > > > >> b[0] > > > > => 48 > > > > >> b[0].chr > > > > => "0" > > > > > > Hope that helps, > > > > Matt > > > > > > On Sun, 2008-07-13 at 06:01 -0700, KathysK...@gmail.com wrote: > > > > > I've got a math function where I'm converting a base36 number to a > > > > > base 10 integer and it works wonderfully, except when the first > > > > > character is a zero. > > > > > I could get this method to work if I could look at the first > position > > > > > in a string, and if it was a zero, I'd then add a zero to the first > > > > > position of the resulting string. > > > > > Here's the equation (and its compliment); > > > > > > > def calc_ovalue(barcode) > > > > > barcode.to_i(36) > > > > > end > > > > > > > def calc_other(barcode) > > > > > onum = calc_ovalue(barcode) > > > > > if onum.odd? > > > > > (onum + 1).to_s(36).upcase > > > > > else > > > > > (onum - 1).to_s(36).upcase > > > > > end > > > > > end > > > > > > > some examples of accepeting the first value and calculating the > second > > > > > two are; > > > > > 0N5F 0N5G ovalue = 30003 > > > > > 0V9B 0V9C ovalue = 40511 > > > > > 6JYV 6JYW ovalue = 305815 > > > > > > > Thank you, > > > > > Kathleen > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
How about this? All operations here should be (fairly) cheap. def next_barcode(barcode) length = barcode.length val = barcode.to_i(36) if val.odd? val += 1 else val -= 1 end ret = val.to_s(36).upcase (''0'' * (length - ret.length)) + ret end tests = [ ''AAAAAA'', ''AAAAAZ'', ''012345'', ''00000Z'', ''00ZZZZ'' ] tests.each do |test| puts "#{test} --> #{next_barcode(test)}" end --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---