develooper Front page | perl.beginners | Postings from October 2019

perl script problem

From:
=?GBK?B?wfW2qw==?=
Date:
October 14, 2019 05:46
Subject:
perl script problem
Message ID:
36794b24.4135.16dc80d92c7.Coremail.liudongm02@163.com
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

 
格言:积土而为山,积水而为海;
    知识在于积累,成功在于勤奋。




 


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About