<p>given the request on way would be the following:</p>
<pre><code>getIdFromTokenName :: TokenName -&gt; Integer
getIdFromTokenName tn = getIdFromStr 0 $ (BS8.unpack . unTokenName) tn -- BS8.unpack :: ByteString -&gt; String
  where
    getIdFromStr :: Integer -&gt; String -&gt; Integer
    getIdFromStr accumulatedNum strName =
      case strName of
        (char: restStr) -&gt;
          case maybeIntegerFormChar char of
            Nothing  -&gt; getIdFromStr accumulatedNum restStr
            Just int -&gt; getIdFromStr (10 * accumulatedNum + int) restStr
        _               -&gt; accumulatedNum

    maybeIntegerFormChar :: Char -&gt; Maybe Integer
    maybeIntegerFormChar ch =
      case ch of
        '0' -&gt; Just 0
        '1' -&gt; Just 1
        '2' -&gt; Just 2
        '3' -&gt; Just 3
        '4' -&gt; Just 4
        '5' -&gt; Just 5
        '6' -&gt; Just 6
        '7' -&gt; Just 7
        '8' -&gt; Just 8
        '9' -&gt; Just 9
        _   -&gt; Nothing
</code></pre>
<p>this should satisfy the codition of getting out <code>123</code> from <code>&quot;AAA123&quot;</code> but note that <code>&quot;AAA123BB45&quot;</code> will result in <code>12345</code></p>

CC BY-SA 4.0
2022-07-05T12:39:21.147Z
I like the addition of maybe, but would like to add that his might increase the script size. Another option to still be on the safe side is to first check the policy id and then use the unsafe `getIdFromStr` function. If the minting of the names is federated the occurrence of the edge case is not a problem.
Fermat
CC BY-SA 4.0
2022-06-05T09:34:56.270Z
2022-06-05T09:34:56.270Z
Michele Nuzzi
<p>Token name is a Bytestring therefore it has to handle like that. I am learning and my attempt at extracting the integer value is like this:-</p>

<pre><code>import Data.Char ( ord )
import Data.ByteString.Char8 (ByteString, unpack,pack)

getIdFromTokenName :: ByteString -&gt; Integer
getIdFromTokenName tn = numbers (reverse (unpack tn)) 0 1

numbers :: String-&gt;Integer-&gt;Integer -&gt; Integer 
numbers [] x y = x
numbers (l:ls) x y = if ord l &gt;= 48 &amp;&amp; ord l &lt;= 57
    then numbers ls ((toInteger (ord l) - 48)* y + x) (y*10)
    else numbers ls x y
</code></pre>
<p>I used pack unpacked from the module imported above in ghci it was working like this</p>
<pre><code>*Main&gt; getIdFromTokenName (pack &quot;NUM123&quot;) 
123
</code></pre>
<p>This is not a safe function to extract numbers and is also not the best method I feel. It's not checked in the Plutus setting also.</p>

CC BY-SA 4.0
2021-11-17T06:29:48.050Z
2021-11-17T06:37:06.053Z
2021-11-17T06:37:06.053Z
raghu
<p>How can I make the following function to retrieve only digit portion from the TokenName and use it on chain?</p>
<p>For example I want to retrieve the number 123 from &quot;AAA123&quot; (TokenName)</p>
<pre><code>{-# INLINABLE getIdFromTokenName #-}
getIdFromTokenName :: TokenName -&gt; Integer
getIdFromTokenName tn = ...
</code></pre>
<p>and I have the check function for state machine, as follows:</p>
<pre><code>{-# INLINABLE check #-}
check :: Game -&gt; GameDatum -&gt; GameRedeemer -&gt; ScriptContext -&gt; Bool
check g _ (Buy tn) _ = ix &gt; 0 &amp;&amp; gMaxItems g &gt;= ix
                            where
                                ix = getIdFromTokenName tn
</code></pre>

<plutus><smart-contract><haskell><on-chain>
Retrieve only digit from TokenName and use onchain
# Retrieve only digit from TokenName and use onchain

(Asked by: username_0 on 2021-11-17)

How can I make the following function to retrieve only digit portion from the TokenName and use it on chain?
For example I want to retrieve the number 123 from "AAA123" (TokenName)
{-# INLINABLE getIdFromTokenName #-}
getIdFromTokenName :: TokenName -> Integer
getIdFromTokenName tn = ...

and I have the check function for state machine, as follows:
{-# INLINABLE check #-}
check :: Game -> GameDatum -> GameRedeemer -> ScriptContext -> Bool
check g _ (Buy tn) _ = ix > 0 && gMaxItems g >= ix
                            where
                                ix = getIdFromTokenName tn

**Tags:** plutus, smart-contract, haskell, on-chain

---

## 2 answers

### Answer by username_1 on 2022-06-05. Score: 1

given the request on way would be the following:
getIdFromTokenName :: TokenName -> Integer
getIdFromTokenName tn = getIdFromStr 0 $ (BS8.unpack . unTokenName) tn -- BS8.unpack :: ByteString -> String
  where
    getIdFromStr :: Integer -> String -> Integer
    getIdFromStr accumulatedNum strName =
      case strName of
        (char: restStr) ->
          case maybeIntegerFormChar char of
            Nothing  -> getIdFromStr accumulatedNum restStr
            Just int -> getIdFromStr (10 * accumulatedNum + int) restStr
        _               -> accumulatedNum

    maybeIntegerFormChar :: Char -> Maybe Integer
    maybeIntegerFormChar ch =
      case ch of
        '0' -> Just 0
        '1' -> Just 1
        '2' -> Just 2
        '3' -> Just 3
        '4' -> Just 4
        '5' -> Just 5
        '6' -> Just 6
        '7' -> Just 7
        '8' -> Just 8
        '9' -> Just 9
        _   -> Nothing

this should satisfy the codition of getting out 123 from "AAA123" but note that "AAA123BB45" will result in 12345

> **Comments:**
> **username_2**: I like the addition of maybe, but would like to add that his might increase the script size. Another option to still be on the safe side is to first check the policy id and then use the unsafe `getIdFromStr` function. If the minting of the names is federated the occurrence of the edge case is not a problem.  

---

### Answer by username_3 on 2021-11-17. Score: 0

Token name is a Bytestring therefore it has to handle like that. I am learning and my attempt at extracting the integer value is like this:-
import Data.Char ( ord )
import Data.ByteString.Char8 (ByteString, unpack,pack)

getIdFromTokenName :: ByteString -> Integer
getIdFromTokenName tn = numbers (reverse (unpack tn)) 0 1

numbers :: String->Integer->Integer -> Integer 
numbers [] x y = x
numbers (l:ls) x y = if ord l >= 48 && ord l <= 57
    then numbers ls ((toInteger (ord l) - 48)* y + x) (y*10)
    else numbers ls x y

I used pack unpacked from the module imported above in ghci it was working like this
*Main> getIdFromTokenName (pack "NUM123") 
123

This is not a safe function to extract numbers and is also not the best method I feel. It's not checked in the Plutus setting also.


---

