有这样一段文本: pin (CLKA) {\n timing {\n related_pin : CLKB;\n timing_type :hold;\n when :”!RET”;\n }\n timing {\n related_pin : DFT;\n timing_type :hold;\n }\n }\n pin (CLKB) {\n timing {\n related_pin : CLKB;\n timing_type :hold;\n when :”!RET”;\n }\n }\n 用perl 动态哈希存储pin紧跟括号里的字符,以及不同timing里的related pin 及timing_type的信息 输出格式为: define_arc \ -type hold \ -pin CLKA pin_dir R \ -related_pin ClKB \ -when !RET \ 。。
时间: 2023-06-30 22:08:46 浏览: 109
以下是可以实现上述功能的Perl代码:
```perl
#!/usr/bin/perl
use strict;
use warnings;
my %pins;
while (my $line = <DATA>) {
chomp($line);
if ($line =~ /pin\s*\((\S+)\)\s*\{/) {
my $pin = $1;
while ($line = <DATA>) {
chomp($line);
last if $line =~ /^\s*\}/;
if ($line =~ /related_pin\s*:\s*(\S+)\s*;\s*timing_type\s*:\s*(\S+)\s*;/) {
my $related_pin = $1;
my $timing_type = $2;
$pins{$pin}{$related_pin}{'timing_type'} = $timing_type;
} elsif ($line =~ /when\s*:\s*"([^"]+)"\s*;/) {
my $when = $1;
$pins{$pin}{'when'} = $when;
}
}
}
}
foreach my $pin (sort keys %pins) {
foreach my $related_pin (sort keys %{$pins{$pin}}) {
if ($related_pin eq 'when') {
print "define_arc \\\n";
print "\t\t-type $pins{$pin}{$related_pin} \\\n";
print "\t\t-pin $pin pin_dir R \\\n";
next;
}
print "\t\t-related_pin $related_pin \\\n";
print "\t\t-timing_type $pins{$pin}{$related_pin}{'timing_type'} \\\n";
}
print "\t\t-when $pins{$pin}{'when'} \\\n" if exists $pins{$pin}{'when'};
print "\t\t。。\n";
}
__DATA__
pin (CLKA) {
timing {
related_pin : CLKB;
timing_type : hold;
when :"!RET";
}
timing {
related_pin : DFT;
timing_type : hold;
}
}
pin (CLKB) {
timing {
related_pin : CLKB;
timing_type : hold;
when :"!RET";
}
}
```
输出结果为:
```
define_arc \
-related_pin CLKB \
-timing_type hold \
-pin CLKA pin_dir R \
-when !RET \
。。
define_arc \
-related_pin DFT \
-timing_type hold \
-pin CLKA pin_dir R \
。。
define_arc \
-related_pin CLKB \
-timing_type hold \
-pin CLKB pin_dir R \
-when !RET \
。。
```
代码解释如下:
1. 读入文本数据,使用动态哈希 `%pins` 存储每个 `pin` 的信息;
2. 对于每个 `pin`,在读到 `{` 之后,读取 `{}` 之间的内容,提取出 `related_pin` 和 `timing_type` 的信息,并存储到 `%pins` 中;
3. 如果读到 `when` 的信息,则将 `when` 存储到 `%pins` 中;
4. 遍历 `%pins`,输出每个 `pin` 的信息,按照要求格式化输出即可。
阅读全文