user4818: 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


user6763: 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

4818: <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>
 6763: <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>
