From CLI, how to enter integers with underscores as 123_456_789

From the CLI, I would like to be able to enter integers as: 123_456_789

I currently used the below code for user data input:

def twinprimes_ssoz() 
  end_num   = {ARGV[0].to_u64, 3u64}.max 
  start_num = ARGV.size > 1 ? {ARGV[1].to_u64, 3u64}.max : 3u64

It takes at least 1 or 2 positive 64-bit unsigned integers as inputs.

./twinprimes_ssoz 98123131 25484942

I’d like to enter the values as: ./twinprimes_ssoz 98_123_131 25_484_942

Can the std library handle this, or do I need to write custom methods?

Pretty sure you can just do like ARGV[1].to_u64 underscore: true.

Ref: String - Crystal 1.7.2

1 Like

That doesn’t work as is.

 225 | end_num   = {ARGV[0].to_u64 underscore true:, 3u64}.max
Error: unexpected token: ","

I think you typed/copy pasted it wrong.

I don’t understand your reply?

I need it to work in the context of the sample code I provided.
The String docs say this:

"12_345".to_i                   # raises ArgumentError
"12_345".to_i(underscore: true) # => 12345

I haven’t been able to manipulate it to run in that form.

Right, and the error said underscore true:, and not underscore: true,. Might also need () around it like you have above ^.

Excuse my dyslexia brought on by early stage dementia from acute old age senility. :smile:

Thanks, this works.

  end_num   = {(ARGV[0].to_u64 underscore: true), 3u64}.max
  start_num = ARGV.size > 1 ? {(ARGV[1].to_u64 underscore: true), 3u64}.max : 3u64
1 Like