|
chris123zugg Apprentice
Joined: 23 Aug 2013 Posts: 194
|
Posted: Sun Dec 29, 2024 11:48 am
Words to numbers building a function or db? |
so this is a seemingly easy one but is beginning to be quite tedius.
There is an item that does a thing, you must look at it to see how many charges are left on the item, the rub is it's spelled out not in a numeric state
One =1, Two = 2, and so on ad infinitum.
Is there a database already in place in cmud i dont know of that has this already done i can use or a function built by someone i also dont know about to do this?
I could use #switch and endlessly input up to 500 of the switches but i cant see myself doing this.:\
anyone with help in this would be amazing, thank you! |
|
|
|
shalimar GURU
Joined: 04 Aug 2002 Posts: 4715 Location: Pensacola, FL, USA
|
Posted: Sun Dec 29, 2024 12:19 pm |
There is no current function built that I know of, but we could build one.
$numStr = "four hundred thousand four hundred twenty four"
$numStr = %replace($numStr, "twenty", 20)
$numStr = %replace($numStr, "four", 4)
$numStr = %replace($numStr, " hundred", "*100")
$numStr = %replace($numStr, " thousand", "*1000")
$numStr = %replace($numStr, " ", "+")
#SAY %eval($numStr)
Just convert it into an equation.
Though the basic order of operations will fail you at numbers greater than one million.
But i think that limitation should be more than enough for this problem. |
|
_________________ Discord: Shalimarwildcat |
|
|
|
chris123zugg Apprentice
Joined: 23 Aug 2013 Posts: 194
|
Posted: Sun Dec 29, 2024 12:41 pm |
I see, how then would i change this to work for any spelled out numbers vs what you have there?
this is the capture for the function you built...
are thirty-nine runes invoking the power and strength of magic.
so i'd have to have 2 seperate ones for the single spelling numbers and the hyphenated numbers...
i should have started with this heh, i apologize. |
|
|
|
shalimar GURU
Joined: 04 Aug 2002 Posts: 4715 Location: Pensacola, FL, USA
|
Posted: Sun Dec 29, 2024 1:28 pm |
Nope, %replace ignores word boundaries, so it's just a matter of replacing things in the right order.
For instance, i don't strip out the spaces in my example until after all the cases that require a space.
Convert the hyphen into a plus sign. |
|
_________________ Discord: Shalimarwildcat |
|
|
|
shalimar GURU
Joined: 04 Aug 2002 Posts: 4715 Location: Pensacola, FL, USA
|
Posted: Sun Dec 29, 2024 1:47 pm |
I went through the trouble of making it into a fully fledged function
Code: |
<func name="w2n" id="52692">
<value>#LOCAL $total
$numStr = %replace( $numStr, " thousand", ")*1000|")
$numStr = %replace( $numStr, " million", ")*1000000|")
#FORALL $numStr {
$segStr=%i
$segStr = %replace( $segStr, "ten", 10)
$segStr = %replace( $segStr, "eleven", 11)
$segStr = %replace( $segStr, "twelve", 12)
$segStr = %replace( $segStr, "thirteen", 13)
$segStr = %replace( $segStr, "fourteen", 14)
$segStr = %replace( $segStr, "fifteen", 15)
$segStr = %replace( $segStr, "sixteen", 16)
$segStr = %replace( $segStr, "seventeen", 17)
$segStr = %replace( $segStr, "eightteen", 18)
$segStr = %replace( $segStr, "nineteen", 19)
$segStr = %replace( $segStr, "twenty", 20)
$segStr = %replace( $segStr, "thirty", 30)
$segStr = %replace( $segStr, "forty", 40)
$segStr = %replace( $segStr, "fifty", 50)
$segStr = %replace( $segStr, "sixty", 60)
$segStr = %replace( $segStr, "seventy", 70)
$segStr = %replace( $segStr, "eighty", 80)
$segStr = %replace( $segStr, "ninty", 90)
$segStr = %replace( $segStr, "one", 1)
$segStr = %replace( $segStr, "two", 2)
$segStr = %replace( $segStr, "three", 3)
$segStr = %replace( $segStr, "four", 4)
$segStr = %replace( $segStr, "five", 5)
$segStr = %replace( $segStr, "six", 6)
$segStr = %replace( $segStr, "seven", 7)
$segStr = %replace( $segStr, "eight", 8)
$segStr = %replace( $segStr, "nine", 9)
$segStr = %replace( $segStr, " hundred", "*100)")
$segStr = %replace( $segStr, "-", "+")
#FORALL $segStr {
$equat = %replace( %trim( %j), " ", "+")
#LOOP (%len($equat)-%len(%replace($equat, ")"))) {$equat=%insert("(", $equat, 1)}
$num=%eval( $equat)
#IF (%number( $num)) {$total=($total+$num)} {
#PRINT error converting:
#PRINT $segStr
#PRINT %j
#PRINT $equat
}
}
}
#RETURN $total</value>
<arglist>$numStr</arglist>
</func>
|
Should work on any number less than a billion now
To use it you would do:
@w2n($numStr) |
|
_________________ Discord: Shalimarwildcat |
|
|
|
shalimar GURU
Joined: 04 Aug 2002 Posts: 4715 Location: Pensacola, FL, USA
|
Posted: Sun Dec 29, 2024 5:21 pm |
#TR {are ([%w -]) runes invoking} {#SUB {are @w2n(%1) runes invoking}}
|
|
_________________ Discord: Shalimarwildcat |
|
|
|
shalimar GURU
Joined: 04 Aug 2002 Posts: 4715 Location: Pensacola, FL, USA
|
Posted: Sun Dec 29, 2024 7:23 pm |
I just had to keep going over my own code...
Good luck finding a valid positive integer in string form from actual gameplay that breaks it.
Code: |
<func name="w2n" id="52692">
<value>#LOCAL $total $equat
$numStr = %replace( $numStr, " thousand", ")*1000|")
$numStr = %replace( $numStr, " million", ")*1000000|")
$numStr = %replace( $numStr, " billion", ")*1000000000|")
$numStr = %replace( $numStr, " trillion", ")*1000000000000|")
$numStr = %replace( $numStr, " quadrillion", ")*1000000000000000|")
$numStr = %replace( $numStr, " quintillion", ")*1000000000000000000|")
#FORALL $numStr {
$segStr=%trim(%i)
$segStr = %replace( $segStr, "ten", 10)
$segStr = %replace( $segStr, "eleven", 11)
$segStr = %replace( $segStr, "twelve", 12)
$segStr = %replace( $segStr, "thirteen", 13)
$segStr = %replace( $segStr, "fourteen", 14)
$segStr = %replace( $segStr, "fifteen", 15)
$segStr = %replace( $segStr, "sixteen", 16)
$segStr = %replace( $segStr, "seventeen", 17)
$segStr = %replace( $segStr, "eightteen", 18)
$segStr = %replace( $segStr, "nineteen", 19)
$segStr = %replace( $segStr, "twenty", 20)
$segStr = %replace( $segStr, "thirty", 30)
$segStr = %replace( $segStr, "forty", 40)
$segStr = %replace( $segStr, "fifty", 50)
$segStr = %replace( $segStr, "sixty", 60)
$segStr = %replace( $segStr, "seventy", 70)
$segStr = %replace( $segStr, "eighty", 80)
$segStr = %replace( $segStr, "ninty", 90)
$segStr = %replace( $segStr, "one", 1)
$segStr = %replace( $segStr, "two", 2)
$segStr = %replace( $segStr, "three", 3)
$segStr = %replace( $segStr, "four", 4)
$segStr = %replace( $segStr, "five", 5)
$segStr = %replace( $segStr, "six", 6)
$segStr = %replace( $segStr, "seven", 7)
$segStr = %replace( $segStr, "eight", 8)
$segStr = %replace( $segStr, "nine", 9)
$segStr = %replace( $segStr, " hundred", "*100)")
$segStr = %replace( $segStr, "-", "+")
$segStr = %replace( $segStr, " ", "+")
#LOOP (%len( $segStr)-%len( %replace( $segStr, ")"))) {$segStr=%insert( "(", $segStr, 1)}
#IF (%number( %eval( $segStr))) {
#IF ($equat) {$equat=%concat($equat, "+" $segStr)} {$equat=$segStr}
} {#PRINT Error converting: $segStr}
}
#IF (%number(%eval($equat))) {#RETURN %eval($equat)} {#PRINT Error with $equat}</value>
<arglist>$numStr</arglist>
</func>
|
After reformatting.... 43 lines to cover up to a sextillion. |
|
_________________ Discord: Shalimarwildcat |
|
|
|
shalimar GURU
Joined: 04 Aug 2002 Posts: 4715 Location: Pensacola, FL, USA
|
Posted: Sun Dec 29, 2024 8:34 pm |
I made it even smaller and turned it into a package in the library, fun little thought experiment.
String to Number
under Utility |
|
_________________ Discord: Shalimarwildcat |
|
|
|
chris123zugg Apprentice
Joined: 23 Aug 2013 Posts: 194
|
Posted: Sun Dec 29, 2024 9:02 pm |
Awesome! thanks shalimar!
now to use this function i simply use the command -> @w2n($numStr)
inside my capture of -> are thirty-nine runes invoking the power and strength of magic.
and it will turn whatever the runes say there are into numerals?
i deleted the old function and copied all of the new one and the capture to use.
it doesnt #say anything :\
I just also saw that you created a variable i could have captured from as well in the original capture/function.
lollll however it completely replaced the text output INLINE with the captured portion :D
I have never once seen that work like this, mind is now blown. how would i then save this output into a variable i can use for other actions?
well apparently it is captureable again, so i can actually manage this portion! |
|
|
|
shalimar GURU
Joined: 04 Aug 2002 Posts: 4715 Location: Pensacola, FL, USA
|
Posted: Mon Dec 30, 2024 12:41 am |
You can assign the value to another variable just like always.
var=@w2n(string)
P.S. #SUB is supposed to substitute the text |
|
_________________ Discord: Shalimarwildcat |
|
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|