perl下关于文件读写,hash统计频数并排序的总结

perl下关于文件读写,hash统计频数并排序的总结,第1张

概述很少用到perl,这次用了一把,特意记录一下关于文件读写和hash统计频数排序的总结: 1.文件读写 perl下读写文件非常简单:,首先是读: #打开文件 open(FILE_NAME, $_)||die "can't open part-m file"; 一行一行读出来并处理: while (<FILE_NAME>)   {    chomp;    print $_,"\n";    ###

很少用到perl,这次用了一把,特意记录一下关于文件读写和hash统计频数并排序的总结:

1.文件读写

perl下读写文件非常简单:,首先是读:

#打开文件

open(file_name,$_)||dIE "can't open part-m file";

一行一行读出来并处理:

while (<file_name>)
  {
   chomp;
   print $_,"\n";

   ###

   处理 

  ###

}

然后是写:

#创建写的文件:不存在就创建,存在就清空然后再写;如果将">"改成">>"就是追加的写

open(OUTfile1_List,">$resultpath.\SpecificResult.txt")||dIE "can't open file SpecificResult.txt!";

#一行行写文件

print OUTfile1_List ("$lineTxt\n");

#写完关闭

close(OUTfile1_List);

 

2.哈希统计频数并排序

#初始化

my %hash=();

#统计每个词的个数

$hash{$lineTxt}++;

#按value值排序

my @keys = sort { $hash{$b} <=> $hash{$a} } keys %hash; #@key里头存的是按哈希的数值大小排序后的键

#按key值排序

my @keys = sort { $b <=> $a} keys %hash; #@key里头存的是按哈希的数值大小排序后的键

 foreach(@keys)
 {
  print OUTfile2_List ("$_"."\t"."$hash{$_}"."\n");
 }

 下面是我写的用来解析现网数据并排序的源代码:

#!/usr/bin/perl -W
##################
# file:
# Author:
# license:

use strict;
use warnings;
use enCoding 'gbk';   # 系统默认编码为GBK
use open IN=>':enCoding(utf8)';   # 读入文件时认为数据按UTF-16编码,自动根据BOM头判断是LE还是BE
use Encode;
use file::Path;
use TIE::file;

#读取外部传入的待解析现网数据的存放目录路径
my $dirpath="";
if(@ARGV == 1)
{
 $dirpath = $ARGV[0];
}else
{
 print "< .pl >   <待解析现网数据的存放目录路径>\n";
 exit(0);
}
print "dir path: ${dirpath}\n";
#$dirpath="E:\\vIDeo_network_data";
my @filearray=(); #存放每个part-m文件的绝对路径
my $filecount = 0; #存放part-m文件的个数
######  遍历文件夹   #####
sub parse_env {   
    my $path = $_[0];
    my $subpath;
    my $handle;
    if (-d $path) {#当前路径是否为一个目录
        if (opendir($handle,$path)) {
           while ($subpath = readdir($handle)) {
                 if (!($subpath =~ m/^\.$/) and !($subpath =~ m/^(\.\.)$/)) {
                  my $p = $path."/$subpath";
                     if (-d $p) {
                        parse_env($p);
                   }
                     else {
                      if($p=~m/part-m/) {
                    push(@filearray,$p);
                    $filecount++;
                      }
                     }
                 }               
          }
        }
        closedir($handle);           
      }
      return  $filecount;
}

my %hash=();
my $filenum=parse_env $dirpath;
if($filenum > 0) #存在part-m文件
{
 print "There are $filenum part-m files!!","\n";
 my $resultpath=$dirpath."
\parse-result";  mkdir($resultpath)  unless(-d $resultpath); #创建目录,准备存放解析结果  open(OUTfile1_List,">$resultpath.\SpecificResult.txt")||dIE "can't open file SpecificResult.txt!";  open(OUTfile2_List,">$resultpath.\FrequencyResult.txt")||dIE "can't open file FrequencyResult.txt!";  foreach(@filearray)  {   #print $_,"\n";   open(file_name,$_)||dIE "can't open part-m file";   while (<file_name>)   {    my @strList=split("\t",$_);    if(($#strList+1)>=4)    {     my $lineTxt=$strList[3];     print OUTfile1_List ("$lineTxt\n");     $hash{$lineTxt}++;    }       }   }  my @keys = sort { $hash{$b} <=> $hash{$a} } keys %hash;  #sort the hash table     foreach(@keys)  {   print OUTfile2_List ("$_"."\t"."$hash{$_}"."\n");  }    close(OUTfile1_List);  close(OUTfile2_List);  print "-------FINISH!!!--------\n"; }

总结

以上是内存溢出为你收集整理的perl下关于文件读写,hash统计频数并排序的总结全部内容,希望文章能够帮你解决perl下关于文件读写,hash统计频数并排序的总结所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/1280620.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存