0

我只是想提取每日数据来分析每年的一些东西。

所以,我制作了一个用于搜索文件夹和文件的代码。

之后,我想在中间具有相同名称的几个文件中提取窗帘线。

当我完成我的工作时,我意识到只剩下一天的信息

每日数据是这样的网格格式。

ncols           751
nrows           601
xllcorner       124.5
yllcorner       33.
cellsize        0.01
nodata_value    -99
 -99.0 -99.0 -99.0 -99.0 -99.0 

我想用我的代码得到这样的结果。

1.txt (2011)
10 10 10 10 10 4 4 3 2
5 4 3 2 10 4 4 3 2
1 1 10 10 10 10 10 10

2.txt (2012)
3 4 2 10 10 4 4 3 2
5 4 3 2 10 4 4 3 2
1 1 10 10 10 10 10 10

use 5.010;
use warnings;

if( $#ARGV < 0 )
  { die "need folder.\n"; }

$dirName = shift(@ARGV);
local($i);

#rutine 
&readSubDir($dirName);

sub readSubDir 
{
    if(!opendir(dirHandle, $_[0]))
    {
        print "$_[0] Failed opening.\n";
        return 0;
    }

    local(@files) = readdir(dirHandle);

    local($i);
    local($newFile);

    local(@dironly);
    local(@fileonly);

    for($i = 0; $i <= $#files; $i++)
    {
        $newFile = $_[0]."\\".$files[$i];
        if(-d $newFile)
        {
        push(@dironly, $files[$i]);
        }
        elsif(-f $newFile)
        {
            push(@fileonly, $files[$i]);
        }
        else
        {}
    }

    @files = @dironly;
    push(@files, @fileonly);
    closedir dirHandle;


    my $cnt = 1;
    my $b = 2011;

    for($i =0; $i <= $#files; $i++){
        $newFile = $_[0]."\\".$files[$i];
        if(-f $newFile){
        open(fileHandle, $newFile)|| die "Cannot open 개체 \n";
        my($dir, $file, $ext) = ($newFile =~ m|^(.*\\)(.*)(\..*)$| );
        if (substr($file,17,4) eq $b){
        while(<fileHandle>){
            if($. == 7){        
        my $filename = $cnt.'.txt';
        open OUT, ">$filename" or die "Failed to create $filename";
        print OUT $_;
        }
        }
        close(fileHandle);
        }
        elsif (substr($file,17,4) eq $b+1){
        $b++;
        $cnt++;
        while(<fileHandle>){
            if($. == 7){        
        my $filename = $cnt.'.txt';
        open OUT, ">$filename" or die "Failed to create $filename";
        print OUT $_;
        }
        }
        close(fileHandle);
        }
        }
        close(OUT);
        }
}
4

1 回答 1

0

这个问题真的不清楚你想要完成什么,因为每日文件数据示例中的信息与输出示例中的任何数据都不匹配。但是,通过查看您的代码,我想我得到了您正在尝试做的事情,并且我认为当您打开输出文件以存储您提取的行时,您的问题正在发生。您正在使用>打开文件,这表示您正在打开文件以进行输出,但如果文件已经存在,它也会覆盖该文件。所以你的代码只是一遍又一遍地覆盖同一个文件,只有最后一个文件中的信息会被保存。您需要使用>>以附加模式打开文件。因此,您的代码应类似于以下内容:

 open OUT, ">>$filename" or die "Failed to create $filename";
于 2013-05-15T11:14:38.223 回答