- 论坛徽章:
- 6
|
本帖最后由 stanley_tam 于 2013-07-24 10:15 编辑
偶试试,正好100行,呵呵- #!/usr/bin/perl
- use strict;
- use warnings;
- use IO::Dir;
- sub get_txt_from;
- sub get_fh;
- sub strip;
- sub process;
- sub main;
- our %interval_g_counter_for = ();
- our %values_of_g_for = ();
- our $containing_folder = 'A';
- main();
- sub main {
- my $texts_aref = get_txt_from($containing_folder);
- for my $file (@{$texts_aref}){
- process($file);
- }
- # 0-5,6-10,11-15,16-20
- for my $key (keys %interval_g_counter_for){
- my $value = $interval_g_counter_for{$key};
- print "$key => [$value]\n";
- }
- # 11 22 33 44
- for my $key (sort keys %values_of_g_for){
- my @values_g = @{ $values_of_g_for{$key} };
- print "When B is $key, the values for g are [@values_g]\n";
- }
- }
- sub process {
- my ($file) = @_;
- my $fh = get_fh($file);
- while(defined(my $line = <$fh>)){
- $line = strip($line);
- next if !$line;
- # skip lines not start with 11 22 33 44
- if ($line =~ m{^ (:? [1234]) \1 \s+}mix) {
- my ($b, $g) = (split /\s+/, $line)[0, 5];
- $b ||= 0;
- $g ||= 0;
- if ($g <= 5) {
- $interval_g_counter_for{'0to5'}++;
- }
- elsif ($g <=10) {
- $interval_g_counter_for{'5to10'}++;
- }
- elsif ($g <=15) {
- $interval_g_counter_for{'11to15'}++;
- }
- elsif ($g <=20) {
- $interval_g_counter_for{'16to20'}++;
- }
- else {
- $interval_g_counter_for{'21to'}++;
- }
- # hash values are array reference, auto vivification...
- push @{$values_of_g_for{$b}}, $g;
- }
- }
- close $fh;
- }
- sub get_txt_from {
- my ($dir) = @_;
- my @files = ();
- my $d_fh = IO::Dir->new($dir);
- if (defined $d_fh) {
- while ( defined(my $file = $d_fh->read) ) {
- next if $file eq '.';
- next if $file eq '..';
- next if $file !~ m{ \.txt $}mix;
- push @files, "$dir/$file";
- }
- }
- return \@files;
- }
- sub get_fh {
- my ($file, $mode) = @_;
- open my $fh, $mode, $file or
- die "Fail to open file $file:$!\n";
- return $fh;
- }
- sub strip {
- my ($str) = @_;
- chomp $str;
- $str =~ s{^\s+ | \s+$}{}gmx;
- return $str;
- }
- __END__
复制代码 |
|