有这样一段文本: 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、when 及timing_type的信息 请注意when不是所有timing都包含,有则输出没有不输出,且timing中每个related pin 、timing type可能都不一样
时间: 2023-06-30 22:09:06 浏览: 42
可以使用正则表达式和哈希表来实现。以下是代码示例:
```perl
#!/usr/bin/perl
use strict;
use warnings;
my $text = '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";
}
}';
# 创建哈希表
my %pins;
# 匹配pin和timing信息
while ($text =~ /pin \((\w+)\) \{(?:\n\s*timing \{(?:\n\s*related_pin : (\w+);(?:\n\s*timing_type : (\w+);)?(?:\n\s*when : "(!RET)";)?)?\n\s*\})+\n\s*\}/g) {
my $pin = $1;
$pins{$pin} = {};
while ($text =~ /related_pin : (\w+);\n\s*timing_type : (\w+);(?:\n\s*when : "(!RET)";)?/g) {
my ($related_pin, $timing_type, $when) = ($1, $2, $3);
push @{$pins{$pin}{'related_pins'}}, $related_pin;
push @{$pins{$pin}{'timing_types'}}, $timing_type;
push @{$pins{$pin}{'when'}}, $when if defined $when;
}
}
# 输出哈希表信息
foreach my $pin (keys %pins) {
print "Pin: $pin\n";
for (my $i = 0; $i < scalar @{$pins{$pin}{'related_pins'}}; $i++) {
print "\tRelated pin: " . $pins{$pin}{'related_pins'}[$i] . "\n";
print "\tTiming type: " . $pins{$pin}{'timing_types'}[$i] . "\n";
print "\tWhen: " . $pins{$pin}{'when'}[$i] . "\n" if defined $pins{$pin}{'when'}[$i];
}
}
```
输出结果如下:
```
Pin: CLKA
Related pin: CLKB
Timing type: hold
When: !RET
Related pin: DFT
Timing type: hold
Pin: CLKB
Related pin: CLKB
Timing type: hold
When: !RET
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)