TCL script to capture range of lines and create two independent variables

Hi I am having a code as stated below

module abcd( a , b , c ,da , fa, na , ta , ma , ra ,
              ta, la , pa );

input a , b, da ,fa , na , ta , ma; 
output c , ra ,ta , 
          la ,pa ;
wire a , b , da , fa ,na ,
        ta , ma;


// MBIST Structures
mbist_hsm_p::mbist_out_hsm_t         mbist_out_hsm;
mbist_hsm_p::mbist_in_hsm_t          mbist_in_hsm;

// HMS 
kkkks ;

jsskks;

endmodule 

Need to take the range between "MBIST Structures " and "//" and the take the first line as a input variable and second line as a output variable.

For example , I am trying below stated code

proc findrange {data start {stop ;}} {
    # Find the starting pattern
    set x1 [string first $start $data]
    if {$x1 < 0} {
        # Pattern not found
        return
    }
    # Skip the pattern
    incr x1 [string length $start]
    # Find the ending pattern after the starting position
    set x2 [string first $stop $data $x1]
    if {$x2 < 0} {
        # Return the remainder of the data when no ending pattern is found
        return [string range $data $x1 end]
    } else {
        # Return the text between the starting and ending patterns
        return [string range $data $x1 [expr {$x2 - 1}]]
    }
}


set chan [open "mode.v"]
set code [read $chan]
close $chan

set var4 [ findrange $code "MBIST Structures" \/\/]

echo $var4 is printing these variables

mbist_hsm_p::mbist_out_hsm_t         mbist_out_hsm;
mbist_hsm_p::mbist_in_hsm_t          mbist_in_hsm;

```
I want to have two lists

$input should be "mbist_hsm_p::mbist_out_hsm_t         mbist_out_hsm;"
$output should be "mbist_hsm_p::mbist_in_hsm_t          mbist_in_hsm;"

How to create these variables from the var4 variable

When I am trying out to print out the $var4 variable , it is printing 4 independent variables

foreach p $var4 {
echo $p
}



mbist_hsm_p::mbist_out_hsm_t
mbist_out_hsm;
mbist_hsm_p::mbist_in_hsm_t
mbist_in_hsm;
Rather it should be " mbist_hsm_p::mbist_out_hsm_t mbist_out_hsm;"
and other one should be "mbist_hsm_p::mbist_in_hsm_t mbist_in_hsm;"

Two lists I am looking for
$input and $output

You could split on newline before doing a foreach like this:

foreach p [ split $var4 "\n" ] {
   puts $p
}
1 Like