Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

perl script problem

4 views
Skip to first unread message

刘东

unread,
Oct 14, 2019, 2:00:05 AM10/14/19
to begi...@perl.org
Dear every one,
I have written a perl script to get seprated files, but finally I every file included multiple files appeared before,
for example, 1 file 2M, 2 file 5 M( included last one), 3 file 6 M (included last two ones), ...
but I expected as 1 file 2M, 2 file 3M, 3 file 1 M, ...
the script as follows:
#! /usr/bin/perl

use strict;
use warnings;
use Getopt::Long;

my ($dir, $files, $file_dir, $file_name, $file_main, $file_format, $outfile);
my %hash;

GetOptions ('dr=s'  =>\$dir);

foreach $files (glob("$dir/*.txt")) {
  ($file_dir, $file_main) = split (/\/\//,$files);
  ($file_name,$file_format) =split(/\./,$file_main);
  $outfile= $file_name."_sg.txt";
 
open OUT,">",$outfile or die "can't open $outfile";
open IN,"<$files" or die "can't read open $files";

while(defined (my $line= <IN>)){
chomp($line);
$line =~s/^\s+//g;
next if ($line =~ m/^S*$/);
my ($id,$start,$end)= (split /\t/,$line)[0,1,2]; # no 3 after "split /\t/,$line"
my $m_lenth = $end-$start+1;
   
    if (exists $hash{$id}) {
        my ($start,$end)= (split /\t/,$hash{$id})[1,2];
        if ($m_lenth > ($end-$start+1)) {
        $hash{$id} = $line;
        }
    } else {
    $hash{$id} = $line;
    }
  }
     
my @val = values %hash;
print OUT "@val\n";    

close OUT;
close IN;
}





--
Hunan Normal University, Changsha, China:Dong Liu
 
格言:积土而为山,积水而为海;
    知识在于积累,成功在于勤奋。


 



 

0 new messages