tcl text processing - rearrange values in rows and columns based on user defined value

后端 未结 3 779
予麋鹿
予麋鹿 2020-12-17 08:27

I am new to tcl and would like to use it in text processing of a simple case. The following format is in Liberty (.lib file) which is used in chip design. I would be truly i

3条回答
  •  悲哀的现实
    2020-12-17 08:35

    The first step is to know how to represent the data in tcl in its final form. This is just one possible solution.

    dict set risedata constraints { 
      constraint {
        0.084 { 0 { 0 1.1 1  6.1 2 11.1 3 16.1 4 21.1 }
                1 { 0 2.1 1  7.1 2 12.1 3 17.1 4 22.1 }
                2 { 0 3.1 1  8.1 2 13.1 3 18.1 4 23.1 }
                3 { 0 4.1 1  9.1 2 14.1 3 19.1 4 24.1 }
                4 { 0 5.1 1 10.1 2 15.1 3 20.1 4 25.1 }
        }
      }
      indexes { 1 { 0.01 0.05 0.12 0.2 0.4 }
                2 { 0.005 0.025 0.06 0.1 0.3 }
                3 { 0.084 0.84 3.36 8.4 13.44 } }
    }
    
    set c 0.084
    puts "$c 2 3: [dict get $risedata constraints constraint $c 2 3]"
    puts "idx1 3: [lindex [dict get $risedata constraints indexes 1] 3]"
    puts "idx2 3: [lindex [dict get $risedata constraints indexes 2] 3]"
    

    Then knowing where you need to be, loading the .lib is just a straightforward parsing problem:

    set fh [open z.lib r]
    set inval false
    while { [gets $fh line] >= 0 } {
      if { [regexp {\);} $line] } {
        set inval false
      }
      if { [regexp {index_(\d+)} $line all idx] } {
        regsub {^[^"]*"} $line {} d
        regsub {".*} $d {} d
        regsub -all {,} $d {} d
        dict set risedata constraints indexes $idx $d
      }
      if { $inval } {
        regsub {^[^"]*"} $line {} d
        regsub {".*} $d {} d
        regsub -all {[ ,]+} $d { } d
        set row [expr {$rcount % 5}]
        set column [expr {$rcount / 5}]
        set i 0
        foreach {v} [split $d { }] {
          set c [lindex [dict get $risedata constraints indexes 3] $i]
          dict set risedata constraints constraint $c $row $column $v
          incr i
        }
        incr rcount
      }
      if { [regexp {values} $line] } {
        set inval true
        set row 0
        set rcount 0
      }
    }
    close $fh
    
    puts $risedata
    set c 0.084
    puts "$c 2 3: [dict get $risedata constraints constraint $c 2 3]"
    puts "idx1 3: [lindex [dict get $risedata constraints indexes 1] 3]"
    puts "idx2 3: [lindex [dict get $risedata constraints indexes 2] 3]"
    

提交回复
热议问题