免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: jiujiujiu338
打印 上一主题 下一主题

帮忙合并两个文件中的不同内容 [复制链接]

论坛徽章:
0
31 [报告]
发表于 2011-08-18 15:48 |只看该作者
回复 32# yinyuemi


    嘿嘿,我个白痴,第一个小问题只要把substr($_,0,index($_,"\.")改为substr($_,0,index($_," ")就行了
现在还有第二个小问题,按照name中的word1,word2,word3排序

论坛徽章:
0
32 [报告]
发表于 2011-08-18 16:37 |只看该作者
回复 34# jiujiujiu338


    第二个小问题我会啦,在foreach my $p  (keys %hash1)  加个(sort {$a cmp $b}就可以了,多谢啊

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
33 [报告]
发表于 2011-08-18 16:46 |只看该作者
回复  jiujiujiu338


    第二个小问题我会啦,在foreach my $p  (keys %hash1)  加个(sort {$a cmp $ ...
jiujiujiu338 发表于 2011-08-18 16:37



sort缺省就是sort {$a cmp $b}
加sort就可以(sort keys %hash1)

论坛徽章:
0
34 [报告]
发表于 2011-08-18 21:05 |只看该作者
回复 32# yinyuemi


   哎,我想按照word1、word2、word3的顺序排序,加上sort keys $hash1后,不对啊,如果有word10,那样word10就在word2前边了,怎么办啊,求解,多谢~

论坛徽章:
0
35 [报告]
发表于 2011-08-18 21:08 |只看该作者
回复 36# jason680


    是是是,多谢提醒

论坛徽章:
0
36 [报告]
发表于 2011-08-19 09:41 |只看该作者
回复 32# yinyuemi


    哎,我想按照word1、word2、word3的顺序排序,加上sort keys $hash1后,不对啊,如果有word10,那样word10就在word2前边了,怎么办啊,求解,多谢~

论坛徽章:
2
射手座
日期:2014-10-10 15:59:4715-16赛季CBA联赛之上海
日期:2016-03-03 10:27:14
37 [报告]
发表于 2011-08-19 13:36 |只看该作者
回复 39# jiujiujiu338
  1. #!/bin/perl
  2. use strict;

  3. open (my $fh1,"file1");
  4. open (my $fh2,"file2");
  5. open (my $fh3,">","file3");

  6. my %hash1;
  7. my %hash2;
  8. my %hash3;
  9. my %hash4;
  10. my $t="";
  11. my $tmp="";

  12. while (<$fh1>){
  13.         if($_!~/end/ and $_!~/^$/){
  14.                 if($_!~/^\d/){
  15.                         $tmp=$1 if(/(name=\S+)\s+.*/);
  16.                         {$t.=$_;}
  17.                 }
  18.                 else{$hash1{$t}.=$_ ;}
  19.                 }
  20.         else{$hash2{$tmp}=$t if($_=~/end/);
  21.                 $t="";
  22.                 $tmp="";
  23.                 }
  24. }
  25. while (<$fh2>){
  26.         if($_!~/end/ and $_!~/^$/){
  27.                 if($_!~/^\d/){
  28.                         $tmp=$1 if(/(name=\S+)\s+.*/);
  29.                         {$t.=$_;}
  30.                 }
  31.                 else{$hash1{$t}.=$_ ;}
  32.                 }
  33.         else{
  34.                 if($_=~/end/){
  35.                         if(exists($hash2{$tmp})){$hash1{$hash2{$tmp}}.=$hash1{$t};delete $hash1{$t};}
  36.                         else{$hash2{$tmp}=$t;}
  37.                 }
  38.                 $t="";
  39.                 $tmp="";
  40.                 }
  41.         }

  42. foreach my $p (keys %hash1) {
  43.         my $key1=$1 if($p=~/.*\nname=word(\d+).*/s);
  44.         $hash4{$key1}.=$p;
  45.         $hash3{substr($_,0,index($_," "))}=$_ foreach (split /\n/,$hash1{$p});
  46.         $hash4{$key1}.=$hash3{$_}."\n" foreach sort {$a<=>$b} (keys %hash3);
  47.         $hash4{$key1}.="end\n\n";
  48.         undef(%hash3);
  49.         }

  50. foreach (sort {$a<=>$b} (keys %hash4)) { print $fh3 $hash4{$_};}
  51.        
  52.        
  53. close($fh1);
  54. close($fh2);
  55. close($fh3);
复制代码

论坛徽章:
0
38 [报告]
发表于 2011-08-20 12:29 |只看该作者
回复 40# yinyuemi


    多谢啊

论坛徽章:
0
39 [报告]
发表于 2011-09-03 21:29 |只看该作者
回复 3# zhlong8


    帮帮忙,好兄弟
求教:下面的程序运行时总是显示Use of uninitialized value $destination in hash element,uninitialized value $bytes,这是咋回事啊?
运行结果:
coconet.dat: 0
coconet.dat=>transferred bytes is 0 bytes
其实coconet.dat 是个文件,我open fh,“coconet.dat”;总说我打开文件句柄只是为了输出,求高人指教
#!/urs/bin/perl -w
use strict;
my %total_bytes;
my $all = "all machines";

while (<coconet.dat>) {
        next if (/^#/);
        my ( $source, $destination, $bytes ) = split;
        $total_bytes{$source}{$destination} += $bytes;
        $total_bytes{$source}{$all}         += $bytes;
}
my @sources =
  sort { $total_bytes{$b}{$all} <=> $total_bytes{$a}{$all} } keys %total_bytes;                                            
for my $source (@sources) {

        my @destinations =
          sort { $total_bytes{$source}{$b} <=> $total_bytes{$source}{$a} }
          keys %{ $total_bytes{$source} };
        print  "$source: $total_bytes{$source}{$all}";
        for my $destination (@destinations) {
                next if $destination eq $all;
                print  "$source=>$destination",
                "transferred bytes is $total_bytes{$sources}{$destination} bytes\n";
                  
        }
        print "\n";
}

论坛徽章:
46
15-16赛季CBA联赛之四川
日期:2018-03-27 11:59:132015年亚洲杯之沙特阿拉伯
日期:2015-04-11 17:31:45天蝎座
日期:2015-03-25 16:56:49双鱼座
日期:2015-03-25 16:56:30摩羯座
日期:2015-03-25 16:56:09巳蛇
日期:2015-03-25 16:55:30卯兔
日期:2015-03-25 16:54:29子鼠
日期:2015-03-25 16:53:59申猴
日期:2015-03-25 16:53:29寅虎
日期:2015-03-25 16:52:29羊年新春福章
日期:2015-03-25 16:51:212015亚冠之布里斯班狮吼
日期:2015-07-13 10:44:56
40 [报告]
发表于 2011-09-03 23:30 |只看该作者
回复  zhlong8


    帮帮忙,好兄弟
求教:下面的程序运行时总是显示Use of uninitialized value $de ...
jiujiujiu338 发表于 2011-09-03 21:29



    读文件的方式错了

open my $fh, '<', 'coconet.dat' or die $!;

while (<$fh>) {
    ...
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP