Chinaunix

标题: 这个程序怎么只部分工作呢?清指出毛病所在 [打印本页]

作者: charseller    时间: 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!!
作者: aaronvox    时间: 2006-07-17 10:46
首先还是用下
use stricts;

$id2==$id1
改成
$id2 eq "$id1"
试试
作者: charseller    时间: 2006-07-17 13:59
哎,差距啊!

谢谢!! 改成 eq 就好了。

自己太疏忽了:(
作者: ulmer    时间: 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 编辑 ]
作者: vio    时间: 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 编辑 ]
作者: vio    时间: 2006-07-17 22:48
diff, comm等命令可以很简单的完成吧,
我不知道perl的命令行里怎么实现像awk一样分别读取两个文件,
perl -ne "..." file1 file2 .. ?
怎么判断读到了第几个文件,例如file2呢?
谢谢。
作者: huaxue    时间: 2006-07-18 14:49
楼上的楼上的,这句话是什么意思啊?

%h=map{$_=>1}@d1
作者: huaxue    时间: 2006-07-18 14:51
哦,是不是对所有的以@d1的数组关联value为1啊?
作者: huaxue    时间: 2006-07-18 14:57
在比较大小方面,eq与==有什么区别吗?
作者: ulmer    时间: 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.
作者: huaxue    时间: 2006-07-19 11:36
谢谢~




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2