0
New Features: 
- Plumbing for DSI dynamic refresh rate change
    - jdflksjdfksjdkfjsdf
    -jdksjfdkjfskjdfskf
Fixes: 
-  Fix KW issue 7449.
- Adding fix to avoid reading EDIDPO twice on each HPD,  EDID cache          
-   Mark layer unused to allow the test to run.

    Fixed CRs: 3847263, 498327498, 92834927

上面的文本在我的文本文件中重复了几次。我想解析每次出现并提取数据以放入下一个文本文件中,该文件将列出“新功能”下的所有新功能、“修复”下的修复和“固定 CR”下的固定 CR。

如何进一步改进我的代码以使其正常工作?现在,输出仅在标题下显示新功能。换句话说,我想重新使用我的子程序myparser()来获取“Fixes:”和:Fixed CRs:之间以及“Fixed CRs:”和行尾输出之间的行:

New Features: 
  -Plumbing for DSI dynamic refresh rate change
  -add watchdog support for 8084 and 8x62
  -Displays supported and the features supported for each of them has been made   chip-specific.
  -Size optimization for eventlog
  -Integrate new power framework from the development sandbox
  -New KMD CAP: supportsDummyPageMapping
  -Adding support for chipID GfxLibChipIDOxili305Dino
  -Using parent driver to handle thermal mitigation request instead of AV stream      

下面是我的代码:

#!perl -w
use strict;
use autodie;
use warnings;

open (FILE_IN,"<S2.txt") or print "Failed to open S2.txt\n" and die;
open FILE_OUT,"+>ramu_15.txt" or print "Failed to open S2.txt\n" and die;

my $a = "New Features:";
my $b = "Fixes:";
my $c = "Fixed CRs:";

sub myparser () {

    my $started       = 0;
    my $printFeatures = 1;

    print "START....\n";
    my @lines = <FILE_IN>;

    foreach (@lines) {

        if ($_ =~ /$a/) {
            if ($printFeatures == 1) {
                print FILE_OUT $_;
                $printFeatures = 0;
            }
            $started = 1;
            next;
        }
        if ($started == 1) {
            if ($_ !~ /$b/) {
                print "$_\n" if $_ ne "\n";
                print FILE_OUT $_ if $_ ne "\n";
            }
            else {
                $started = 0;
            }
        }
    }
}

myparser();
print "...END\n";

close FILE_IN;
close FILE_OUT;
4

1 回答 1

1

显而易见的解决方案是使用哈希。

有一些有代表性的数据来​​测试会有所帮助,但是这个程序应该按照你的要求做。

use strict;
use warnings;
use autodie;

open my $in, '<', 'S2.txt';
open my $out, '>', 'ramu_15.txt';

my %data;
my $key;

while (my $text = <$in>) {
  chomp $text;
  if (  $text =~ /(\w+(?:\s+\w+)*):\s*(.*)/  ) {
    $key = $1;
    $text = $2;
  }
  push @{$data{$key}}, $text if $key and $text =~ /\S/;
}

for ('New Features', 'Fixes', 'Fixed CRs') {
  print "\n$_\n";
  print "$_\n" for @{$data{$_}};
}

输出

New Features
- Plumbing for DSI dynamic refresh rate change
    - jdflksjdfksjdkfjsdf
    -jdksjfdkjfskjdfskf

Fixes
-  Fix KW issue 7449.
- Adding fix to avoid reading EDIDPO twice on each HPD,  EDID cache          
-   Mark layer unused to allow the test to run.

Fixed CRs
3847263, 498327498, 92834927
于 2013-05-25T07:51:12.820 回答