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

perl script with wrong

2 views
Skip to first unread message

刘东

unread,
Oct 13, 2019, 7:45:04 PM10/13/19
to begi...@perl.org
Dear friends,
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
 
格言:积土而为山,积水而为海;
    知识在于积累,成功在于勤奋。


 

Jim Gibson

unread,
Oct 14, 2019, 1:30:05 AM10/14/19
to 刘东, Perl Beginners


> On Oct 10, 2019, at 6:04 PM, 刘东 <liudo...@163.com> wrote:
>
> Dear friends,
> 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, …

You are adding data to %hash for each file read. Since %hash is global, it will contain all of the data from the current file and all previously read files.

If you want to start with an empty hash for each file, then declare %hash inside of the foreach loop or assign %hash to () at the beginning of the foreach loop.
Jim Gibson
J...@Gibson.org
0 new messages