Perl 网络互作去重

2020-02-26  本文已影响0人  挽山

使用前注意修改输出路径和文件名,运行时会在命令行里出现 please put in the file like this f:\perl\data.txt,按照要求输入想要去重的对象所在路径就行了。

#!/bin/perl
use strict;
use warnings;
my $filename;
my %hash;
my @information;
my $key1;
my $key2;
print "please put in the file like this f:\\\\perl\\\\data.txt\n";
chomp($filename=<STDIN>);
open(IN,"$filename")||die("can not open");
while(<IN>)
{
   chomp;
   @information=split/\s+/,$_;
   if(exists $hash{$information[0]}{$information[1]})
   {
       next;
   }
   else
   {
       $hash{$information[0]}{$information[1]}='A';
    }
   }
   close IN;
   open(IN,"$filename")||die("can not open");
   while(<IN>)
   {
       @information=split/\s+/,$_;
       if(exists $hash{$information[1]}{$information[0]})
       {
           delete $hash{$information[0]}{$information[1]}
       }
       else
       {
           next;
       }
   }
   close IN;
   open(OUT,">E:\\毕业课题\\改题\\共表达\\52set-0.26\\52set-DEG-nei-edges-si.txt")||die("can not open");
   foreach $key1 (sort{$a<=>$b} keys %hash)
   {
       foreach $key2 (sort{$a<=>$b} keys %{$hash{$key1}})
       {
           print OUT "$key1"."\t"."$key2"."\n";
       }
   }
close OUT;

perl对于文本处理的能力真不是盖得,perl一点不会以上代码也能用。如果想要实现更多有机会还是学一学语法。

参考:
https://www.jb51.net/article/34992.htm
https://www.cnblogs.com/dtj007/p/5113577.html

上一篇下一篇

猜你喜欢

热点阅读