请教:perl 提取数据的程序,欢迎指点

jacky_xujian 2012-05-27 11:05:30
新手刚学perl语言,最近想写一个小程序,不知道如何入手,就是从文件2中挑选符合要求的位点到文件1中;

文件1:
ID position content

000011 45 haha
000011 122 hehe
000222 50 xixi
000322 200 huhu

文件2:
ID position content

000023 330 zzzz
000011 55 gogo
000011 88 come
000222 400 yyyy
000922 300 xxxxx

其实就是从文件2的第一行开始查看,如果ID相同,position与文件1的比较,若与上下的差值都大于30,则加入到文件1中;
如果没有匹配的ID,则也选该行加入到文件1;预期结果如下(文件1的ID按字符排序,position按数值排序):

000011 45 haha
000011 88 come
000011 122 hehe
000023 330 zzzz
000222 50 xixi
000222 400 yyyy
000322 200 huhu
000922 300 xxxxx

哪位有经验的,希望指点一下,多谢啦!

...全文
205 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
国号汗 2012-06-12
  • 打赏
  • 举报
回复
上面写错了,自己丢板砖了
两个哈希 %hash1 %hash2
以ID做key,分别存储position 和 content
即 $hash1{$id}{"position"} = $position;
$hash1{$id}{"content"} = $content;
%hash2也同样写
用%hash2去找%hash1
foreach $key (sort(keys(%hash2))) {...}
国号汗 2012-06-12
  • 打赏
  • 举报
回复
我是这么想的 两个哈希 %hash1 %hash2
以ID做key,分别存储position 和 content
即 $hash1{$id}{"position"} = $position;
$hash2{$id}{"content"} = $content;
用%hash2去找%hash1
foreach $key (sort(keys(%hash2))) {...}
bugs2k 2012-06-01
  • 打赏
  • 举报
回复
将两个文件的内容加入到 hash 中,id 为关键字,值为二维数组,文件中每行构成一维数组,其他的你自己实现。


#!/usr/bin/env perl
#

use strict;
use warnings;

my $old = shift @ARGV;
my $new = shift @ARGV;
my %hash;

sub do_disp {
my $hash = shift;
for my $key (keys %$hash) {
my $array = $$hash{$key};
print "$key => ";
for my $suba (@$array) {
print "[@$suba] ";
}
print "\n";
}
}

sub do_hash {
my $file = shift;
open my $FD, $file or die $!;
while (<$FD>) {
chomp;
next if !/^\d+/;
my ($id, $pos, $ctx) = split;
if ($hash{$id}) {
push $hash{$id}, [$pos, $ctx];
}
else {
$hash{$id} = [[$pos, $ctx]];
}
}
close $FD;
}

do_hash($old);
do_disp(\%hash);

print '=' x 80, "\n";

do_hash($new);
do_disp(\%hash);
jacky_xujian 2012-05-31
  • 打赏
  • 举报
回复
结果输出还是有问题;

我的思路是,先分别通过哈希获得新旧list的id和id对应的position(可能有多个);

然后针对每个id,操作对应的两个数组;
jacky_xujian 2012-05-31
  • 打赏
  • 举报
回复


#!usr/bin/perl -w

if (@ARGV !=2)
{
print "Usage: perl script new_list old_list\n";
}

$f1=shift;
$f2=shift;

open(FH,$f1) or die "cant open file:$!";
while(<FH>)
{
chomp;
@ar=split;
$hash{$ar[0]}=1;
if (exists $hash{$ar[0]})
{
push @{$pos_new{$ar[0]}},$ar[1];
}
}
close FH;


open(SH,$f2) or die "cant open file:$!";
while(<SH>)
{
@br=split;
if (exists $hash{$br[0]})
{
push @{$pos{$br[0]}},$br[1];
}
}
close SH;

foreach (keys %pos_new)
{
for ($i=0;$i < @{$pos_new{$ar[0]}};$i++)
{
if (!exists $pos{$ar[0]})
{push @{$pos{$ar[0]}},$pos_new{$ar[0]}->[$i];}
elsif (exists $pos{$ar[0]})
{for ($j=0;$j < @{$pos{$ar[0]}};$j++)
{$cha_start = $pos_new{$ar[0]}->[$i] - $pos{$ar[0]}->[0];
$cha_end = $pos_new{$ar[0]}->[$i] - $pos{$ar[0]}->[-1];
if ($cha_start < 0 && abs($cha_start)> 3)
{push @{$pos{$ar[0]}},$pos_new{$ar[0]}->[$i];}
elsif($cha_end > 0 && abs($cha_end) > 3)
{push @{$pos{$ar[0]}},$pos_new{$ar[0]}->[$i];}
else
{$cha1 = $pos_new{$ar[0]}->[$i] - $pos{$ar[0]}->[$j];
$cha2 = $pos_new{$ar[0]}->[$i] - $pos{$ar[0]}->[$j+1];
if($cha1>0 && $cha2<0 && abs($cha1)>3 && abs($cha2)>3)
{push @{$pos{$ar[0]}},$pos_new{$ar[0]}->[$i];}
}
}
}
@{$pos{$ar[0]}}= sort {$a cmp $b || $pos{$a} <=> $pos{$b}} @{$pos{$ar[0]}};
print "$_ @{$pos{$_}}\n";
@{$pos{$ar[0]}}=();
}
}
proorck6 2012-05-30
  • 打赏
  • 举报
回复
排序用sort
jacky_xujian 2012-05-28
  • 打赏
  • 举报
回复
谢谢,已经在写了,到时候如果有问题的话,我再贴脚本出来。
iambic 2012-05-28
  • 打赏
  • 举报
回复
按照你自己的思路写代码就行了。这是最基本的代码执行力。必须学会自己解决。

37,741

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧