• Skip to main content
  • Skip to search
  • Skip to footer
Cadence Home
  • This search text may be transcribed, used, stored, or accessed by our third-party service providers per our Cookie Policy and Privacy Policy.

  1. Community Forums
  2. Custom IC SKILL
  3. Surely this function exists...

Stats

  • Locked Locked
  • Replies 4
  • Subscribers 143
  • Views 13958
  • Members are here 0
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Surely this function exists...

CADcasualty
CADcasualty over 6 years ago

I'm looking for a function that simply accepts a textual input and if the text can be numerically evaluated then return the evaluation, otherwise just return the string. Darned if I can find a function for this!

I've see evalstring() but it errors out too easily i.e. 

evalstring("1")
1
>
evalstring("fred")
*Error* eval: unbound variable - fred  <------------ yuck
>
evalstring("1+1")
2

Then I tried cdfParseFloatString():

cdfParseFloatString("1")

1.0
>
cdfParseFloatString("fred")
"fred"
>
cdfParseFloatString("1+1")
nil   <--------------yuck

So I wrote my own procedure:

procedure( myParse(myvar) if(errsetstring(myvar) evalstring(myvar) myvar) )

myParse("1")
1
>
myParse("fred")
"fred"
>
myParse("1+1")
2
>
myParse("1+fred")
"1+fred"


myParse does what I want without issue, but I'm loathed to use it because I just can't believe something like this doesn't already exist in skill. Can somebody please point me to the right function (again, assuming it exists).

  • Cancel
  • Andrew Beckett
    Andrew Beckett over 6 years ago

    There's nothing that quite does that. Not sure why you think there should be - the requirement to return the original string if you can't evaluate it is not something that universally useful.

    I can simplify your function using:

    procedure(myParse(var) car(errsetstring(var))||var)

    Regards,

    Andrew.

    • Cancel
    • Vote Up +1 Vote Down
    • Cancel
  • CADcasualty
    CADcasualty over 6 years ago in reply to Andrew Beckett

    Thanks Andrew. I guess we have different opinions about how universally useful this function might be :-). Anyway, thanks for your simplified code. It took me a minute to understand why it wouldn't break if errsetstring was successful - if it returns something other than nil then there is no reason to continue with the OR function evaluation. I like the way you think! However in this case it crossed my line of readability vs functionally. Hopefully the entropy of the universe won't be overly impacted if I stick with my original code instead of yours :-).

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Andrew Beckett
    Andrew Beckett over 6 years ago in reply to CADcasualty

    Yes, this is a short-circuiting or operation - it only evaluates the second part if the first part is nil. The benefit is that it doesn't evaluate the expression twice - that might be a problem if the expression has side effects - for example:

    a=10
    var="a=a+5"
    myParse(var)

    In my case, it would return 15, whereas in your case it would return 20 because it got evaluated twice. 

    However, in normal situations you'd not have such a problem.

    Regards,

    Andrew.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • CADcasualty
    CADcasualty over 6 years ago in reply to Andrew Beckett

    Thanks for going the extra mile with this response. I always learn something new every time you post!

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel

Community Guidelines

The Cadence Design Communities support Cadence users and technologists interacting to exchange ideas, news, technical information, and best practices to solve problems and get the most from Cadence technology. The community is open to everyone, and to provide the most value, we require participants to follow our Community Guidelines that facilitate a quality exchange of ideas and information. By accessing, contributing, using or downloading any materials from the site, you agree to be bound by the full Community Guidelines.

© 2025 Cadence Design Systems, Inc. All Rights Reserved.

  • Terms of Use
  • Privacy
  • Cookie Policy
  • US Trademarks
  • Do Not Sell or Share My Personal Information