Perl 使用 sort, hash解析文件代码
来源:互联网 发布:python 不包含字符串 编辑:程序博客网 时间:2024/05/15 04:30
同事写的简单的Perl代码
#!/usr/bin/perl -w
use strict;
my $pbid_index = 0;
my $lbid_index = 3;
my $ver_index = 5;
my $uuid_index = 6;
&test();
sub my_sort {
my %hash;
my @keys;
my @sorted_keys;
my $uuid;
my $last_uuid;
open(M_FILE, "<middlefile");
#read files
while (<M_FILE>) {
my @file_line = split(/:/);
my $logid = $file_line[$lbid_index];
my $flag = -1;
if ($file_line[$pbid_index] ne "pbid") {
next;
}
#find uuid
$uuid = $file_line[$uuid_index];
if (!$uuid) {
next;
} elsif (!$last_uuid) {
$last_uuid = $uuid;
} elsif (eof || ($last_uuid ne $uuid)) {
#sort file
@keys = keys %hash;
@sorted_keys = sort {$a <=> $b} @keys;
#open new file
open NEW_FILE, '>file_'.$last_uuid;
#write files
foreach (@sorted_keys) {
print NEW_FILE $hash{$_};
}
#close files
close NEW_FILE;
$last_uuid = $uuid;
undef %hash;
undef @keys;
undef @sorted_keys;
}
if (exists $hash{$logid}) {
my $new_ver = hex($file_line[$ver_index]);
my @old_line = split(/:/, $hash{$logid});
my $old_ver = hex($old_line[$ver_index]);
if ($new_ver > $old_ver) {
$hash{$logid} = $_;
}
} else {
$hash{$logid} = $_;
}
}
close M_FILE;
}
sub sort_uuid {
my $uindex = 7;
my $cmd = "sort -d -t : -k $uindex -o middlefile tmp";
my $TMP;
my @line;
print $uindex;
open $TMP, '>tmp';
while (<>) {
@line = split(/:/);
if ( 'pbid' ne $line[0]) {
next;
}
print $TMP $_;
}
close $TMP;
system($cmd);
system('rm -f tmp');
}
sub test {
&sort_uuid();
&my_sort();
}
- Perl 使用 sort, hash解析文件代码
- Perl技巧 sort函数使用
- perl 解析xml ,json文件及curl工具的使用
- 使用Perl hash来分析大量数据
- Perl 使用引用来访问hash 数组
- Perl (hash)
- perl hash
- perl hash
- perl hash
- perl hash
- perl sort
- Perl Sort
- Perl解析RC,RC2文件
- 使用hash拆分文件
- 如何使用perl解析excel2007
- 使用ftp上传文件代码解析
- 常用hash函数解析及代码
- Perl的数组与hash表使用的对比
- 发现个有趣的东西
- Poechant 快速安装配置教程:Windows + Ecplise CDT + MinGW开发环境
- android图片类型之间的互转
- 朋友给的提示
- hpux 内核管理
- Perl 使用 sort, hash解析文件代码
- HTML完全手册
- C++学习-------类继承之友元和操作符重载
- Linux 内核参数及Oracle相关参数调整
- 超酷的图片轮换效果
- 程序开发基础学习四(boost::signal2 函数学习)
- 在Android的2.2里面调试camera gc0308的心得
- IE8 Jquery.Ajax 编码问题
- oracle database link创建