1

I have the following string:

  set operating_period          "1.86ns"    ; # set dominant default period , from create_clock command in sdc

I would like to get the number out of this. So the result should be 1.86

Any suggestions how to do that in TCL? I tried scan, but obviously I fail =( ...

3
  • Nevermind... I did it! Commented Oct 25, 2013 at 11:35
  • lindex [split [lindex [split $string \"] 1] ns] 0 Commented Oct 25, 2013 at 11:36
  • It's pretty nasty, but it works =) ! Commented Oct 25, 2013 at 11:37

2 Answers 2

2

Use scan:

% set operating_period "1.86ns"
1.86ns
% set x [scan $operating_period %f]
1.86

http://www.tcl.tk/man/tcl8.6/TclCmd/scan.htm

http://www.tcl.tk/man/tcl8.6/TclCmd/format.htm

Sign up to request clarification or add additional context in comments.

10 Comments

Yeah right! But the number is just an example. I want to get an arbitrary number from this position. How to do that?
It works the same: scan 1ns %f -> 1.0, scan 1.8ns %f -> 1.8 etc.
I think you don't understand the problem. If the number is fixed, I don't have to write code to get it... just type it ^^. I'm looking for something like "scan $string %f" and the result is the number inside the string variable. And the string looks like the one above, just the number is not known...
Your genius formula results in above example "0"... That's because scan does not output the scan result directly, but only the number of hits.
So in this case, it did not find anything. Unbelievable, right?!
|
1

Sometimes, when working with particularly ill-formed data (e.g., anything written free-form by people) you have to use a mixture of techniques to extract the data. For example, you can use both regexp and scan:

set inputString "wow yet 183.326ns another float"
if {[scan [regexp -inline {[\d.]+ns} $inputString] "%f" value] == 1} {
    # Found something! It's in $value now
}

The regexp does the extraction (-inline is nice; it makes regexp return what it matched) and scan “extracts the sense” from what was found and stores a sane floating-point number in $value, assuming there was any there in the first place. You might need to tweak the RE to get best results (for example, the current one won't cope with negative numbers right now).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.