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

perl script problem

October 14, 2019 05:46
perl script problem
Message ID:
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>)){
$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

    知识在于积累,成功在于勤奋。 Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About