免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 2022 | 回复: 10
打印 上一主题 下一主题

这个程序怎么只部分工作呢?清指出毛病所在 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-07-17 10:18 |只看该作者 |倒序浏览
很简单的一个程序,见下说明。但工作只有一部分起作用了(见后面的file3输出),很奇怪。清指出毛病所在,谢谢!!

#!/usr/bin/perl
#now the file "file1" have  id , and "file2" have some of id of file1 but also include some new id,
#this script is to find if there is new id in file2, if have, added and put all in file3


$FileList1 = "file1";
$FileList2 = "file2";
$FileList3 = "file3";




open (OF, $FileList1) || die "Cannot open file: $FileList1";
@data1=<OF>;
close(OF);

open (OF, $FileList2) || die "Cannot open file: $FileList2";
@data2=<OF>;
close(OF);
                       
@data3=();
Outer:foreach $id2 (@data2) {
        foreach $id1 (@data1) {
                if($id2==$id1) {next Outer;}
        }
        push (@data3,$id2);
}
foreach $id3 (@data3){
        push (@data1,$id3);
}
open (OF, ">>$FileList3") || die "Cannot open file: $FileList3";
foreach        $id1 (@data1){
                print OF "$id1";
}
close(OF);



File1:
1
1.1
1.10
1.11
1.12
1.13
1.14
1.15
1.16
1.2
1.3
1.4
1.48
1.5
1.6
1.7
1.8
1.9
1.1.1
1.1.2
1.1.3
1.1.4
1.1.5
1.1.6
1.1.1.1
1.1.2.1
1.1.2.2
1.1.2.3
2
2.1
2.3


File2:
1
1.1
1.1.1.1.1.1.1.1
1.10
1.11
1.12
1.13
1.14
1.30
1.33
1.4
2.55
1.48
1.5
1.6
1.7
1.8
1.9
1.30
1.1.1
1.1.2
1.1.3
1.1.4
1.1.5
1.1.6
1.1.1.1
1.1.2.3
2
2.1
2.3



File3:
1
1.1
1.10
1.11
1.12
1.13
1.14
1.15
1.16
1.2
1.3
1.4
1.48
1.5
1.6
1.7
1.8
1.9
1.1.1
1.1.2
1.1.3
1.1.4
1.1.5
1.1.6
1.1.1.1
1.1.2.1
1.1.2.2
1.1.2.3
2
2.1
2.3
1.33
2.55

----------------File3, the new id in file2- 1.33 2.55 were added, but the 1.1.1.1.1.1.1.1 and 1.30 didn't!!

论坛徽章:
0
2 [报告]
发表于 2006-07-17 10:46 |只看该作者
首先还是用下
use stricts;

$id2==$id1
改成
$id2 eq "$id1"
试试

论坛徽章:
0
3 [报告]
发表于 2006-07-17 13:59 |只看该作者
哎,差距啊!

谢谢!! 改成 eq 就好了。

自己太疏忽了:(

论坛徽章:
0
4 [报告]
发表于 2006-07-17 17:53 |只看该作者
原帖由 charseller 于 2006-7-17 13:59 发表
哎,差距啊!

谢谢!! 改成 eq 就好了。

自己太疏忽了:(


Hallo,

using map and hash to simplify the code:
i.e:

  1. perl -le '@d1=qw(1.1 1.2 1.3);@d2=qw(1.1 1.5); %h=map{$_=>1}(@d1,@d2); @d3=sort keys %h; print join "\n", @d3'
复制代码

[ 本帖最后由 ulmer 于 2006-7-19 15:39 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2006-07-17 22:17 |只看该作者
原帖由 ulmer 于 2006-7-17 17:53 发表


Hallo,

using map and hash to simplyfine the code:
i.e:
perl -le '@d1=qw(1.1 1.2 1.3);@d2=qw(1.1 1.5); %h=map{$_=>1}(@d1,@d2); @d3=sort keys %h; print join "\n", @d3'
...
  1. perl -le '@d1=qw(1.1 1.2 1.3);@d2=qw(1.1 1.5); %h=map{$_=>1}@d1; @d3 = map{$_ if !$h{$_}} @d2; print join "\n", @d3'
复制代码
:)

[ 本帖最后由 vio 于 2006-7-17 22:18 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2006-07-17 22:48 |只看该作者
diff, comm等命令可以很简单的完成吧,
我不知道perl的命令行里怎么实现像awk一样分别读取两个文件,
perl -ne "..." file1 file2 .. ?
怎么判断读到了第几个文件,例如file2呢?
谢谢。

论坛徽章:
0
7 [报告]
发表于 2006-07-18 14:49 |只看该作者
楼上的楼上的,这句话是什么意思啊?

%h=map{$_=>1}@d1

论坛徽章:
0
8 [报告]
发表于 2006-07-18 14:51 |只看该作者
哦,是不是对所有的以@d1的数组关联value为1啊?

论坛徽章:
0
9 [报告]
发表于 2006-07-18 14:57 |只看该作者
在比较大小方面,eq与==有什么区别吗?

论坛徽章:
0
10 [报告]
发表于 2006-07-18 15:48 |只看该作者
原帖由 huaxue 于 2006-7-18 14:49 发表
楼上的楼上的,这句话是什么意思啊?

%h=map{$_=>1}@d1


This tricks that set element from array as key in hash, why hash's key is uniq.!
with this method you get only uniq. elem. from an array.

在比较大小方面,eq与==有什么区别吗?


"==" used to compare with NUMMBER, and "eq" for STRING.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP