• 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. Doing math on text.

Stats

  • Locked Locked
  • Replies 3
  • Subscribers 144
  • Views 9205
  • 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

Doing math on text.

MorrisDH
MorrisDH over 3 years ago

Pardon the vague subject title but briefly describing the issue is not easy.

Starting with a string that contains a 'list' of numbers. The word 'list' is in quotes because it is not a SKILL list.

String example: "250 350". There could be any number of elements and the values could be integer or float.

What is needed is to subtract a value for each element and the result is still a single string.

Example: Say we want to subtract 9.5 from each element.

input -> "250 350"

output -> "240.5 340.5"

This is what I came up with and it works fine but it feels a bit ad hoc. Wondering if there could be a better solution.

myString = "250 350"

numList = mapcar( 'evalstring parseString(myString)) ; returns (250 350)

adjustedList = foreach(mapcar x numList x-9.5) ; returns (240.5 340.5)

myNewString = buildString(foreach(mapcar x adjustedList sprintf(nil "%g" x))) ; returns "240.5 340.5"

The above works but would like to see alternative methods

  • Cancel
Parents
  • Andrew Beckett
    Andrew Beckett over 3 years ago

    A cleaner way of setting numList would be this:

    numList=linereadstring(myString)

    and I would probably use lsprintf instead of sprintf since you only want the return value:

    myNewString = buildString(foreach(mapcar x adjustedList lsprintf("%g" x)))

    (potentially using %N rather than %g because in case the result of your operations were integers; can't be in this case because of the subtraction of a floating point, but that's more general).

    Other than that, it's pretty reasonable. The main difference is really using linereadstring as there's no need to involve the evaluator in this. I am always wary of using evalstring or eval when parsing arbitrary strings - for efficiency but also imagine what happens if myString was "exit() 3"?

    Andrew

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
Reply
  • Andrew Beckett
    Andrew Beckett over 3 years ago

    A cleaner way of setting numList would be this:

    numList=linereadstring(myString)

    and I would probably use lsprintf instead of sprintf since you only want the return value:

    myNewString = buildString(foreach(mapcar x adjustedList lsprintf("%g" x)))

    (potentially using %N rather than %g because in case the result of your operations were integers; can't be in this case because of the subtraction of a floating point, but that's more general).

    Other than that, it's pretty reasonable. The main difference is really using linereadstring as there's no need to involve the evaluator in this. I am always wary of using evalstring or eval when parsing arbitrary strings - for efficiency but also imagine what happens if myString was "exit() 3"?

    Andrew

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
Children
  • MorrisDH
    MorrisDH over 3 years ago in reply to Andrew Beckett

    I didn't know about linereadstring. Thanks for the tip. I'll give it a try.

    Morris

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • MorrisDH
    MorrisDH over 3 years ago in reply to Andrew Beckett

    It works perfectly and the code is cleaner. Used linereadstring, lsprintf, and %N.

    • 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