如何删除重复的#include指令?

时间:2013-09-20 21:42:04

标签: perl hash

#include <stdint.h>
#include <stdlib.h>
#include <VideoInChain.h>
#include <VideoOutChain.h>
#include <stdint.h>
#include <stdlib.h>
...//rest of code

我需要删除系统头文件重复而不影响其余代码

我试过

sub uniq {
  my %seen = ();
  my @r = ();
  foreach my $a (@_) {
     unless ($seen{$a}) {           
         push @r, $a;
         $seen{$a} = 1;
      }
    }
  return @r;
  }
  @lines_temp = uniq(@lines_temp);

但它删除了所有类型的重复,包括'(','{'和空格

我只需删除重复的系统文件

1 个答案:

答案 0 :(得分:6)

sub uniq {
  my %seen;
  my @r;
  for my $a (@_) {
     my ($m) = $a =~ /#include\s+<(.+?)>/;
     push @r, $a if !$m or !$seen{$m}++;
  }
  return @r;
}
@lines_temp = uniq(@lines_temp);

sub uniq {
  my %seen;
  return grep {
     my ($m) =~ /#include\s+<(.+?)>/;
     !$m or !$seen{$m}++;
  } @_;
}
@lines_temp = uniq(@lines_temp);

或oneliner,

perl -ne '($m) = /#include\s+<(.+?)>/; print if !$m or !$s{$m}++' file