Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #59532

Re: Rosetta: Increment a numerical string

From rickman <gnuarm@gmail.com>
Newsgroups comp.lang.forth
Subject Re: Rosetta: Increment a numerical string
Date 2017-05-19 01:24 -0400
Organization A noiseless patient Spider
Message-ID <oflvc0$ir2$1@dont-email.me> (permalink)
References <oflpmm$13v1$1@gioia.aioe.org> <oflrbl$8ob$3@dont-email.me> <ddcd9980-4646-4e90-aa96-78dc4b87ae28@googlegroups.com>

Show all headers | View raw


On 5/19/2017 12:47 AM, Julian Fondren wrote:
> On Thursday, May 18, 2017 at 11:16:02 PM UTC-5, rickman wrote:
>> On 5/18/2017 11:44 PM, Robert L. wrote:
>>> "foo99bar".sub(/\d+/){|s| s.to_i + 1}
>>>     ==>"foo100bar"
>>>
>>> "foo99bar".sub(/\d+/){|s| 2**s.to_i }
>>>     ==>"foo633825300114114700748351602688bar"
>>>
>>> In Forth?
>>
>> If this is doing what I think it is doing, I wrote that in an Excel
>> formula... lol
>>
>> =CONCATENATE(LEFT(M84,7),TEXT(1+NUMBERVALUE(MID(M84,8,3)),"000"),"A")
>>
>> Turns "16N048-033A" into "16N048-034A"
>>
>
> Robert's code affects only the first sequence of consecutive digits in
> the string. So it would turn "16N048-033A" into "17N048-033A".
>
> The Rosetta Code task just says "numerical string", though, without
> specifying how non-digits should be considered.
>
> That Excel formula is awfully specific though. It's not any harder to
> be that specific in Forth:
>
>   12 buffer: label
>   s" 16N048-033A" label place
>
>   : excel ( -- )
>     0. label 8 + 3 >number 2drop drop 1+
>     0 <# # # # #> label 8 + swap move ;
>
>   : go 10 0 do cr label count type excel loop ;
>
> Outputs:
>
>   16N048-033A
>   16N048-034A
>   16N048-035A
>   16N048-036A
>   16N048-037A
>   16N048-038A
>   16N048-039A
>   16N048-040A
>   16N048-041A
>   16N048-042A

Yeah, I wasn't trying to do the Rosetta task.  This is something I wrote 
some time ago to generate invoice numbers in a tracking spread sheet.

-- 

Rick C

Back to comp.lang.forth | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Rosetta: Increment a numerical string "Robert L." <No_spamming@noWhere_7073.org> - 2017-05-19 03:44 +0000
  Re: Rosetta: Increment a numerical string rickman <gnuarm@gmail.com> - 2017-05-19 00:16 -0400
    Re: Rosetta: Increment a numerical string Julian Fondren <julian.fondren@gmail.com> - 2017-05-18 21:47 -0700
      Re: Rosetta: Increment a numerical string rickman <gnuarm@gmail.com> - 2017-05-19 01:24 -0400

csiph-web