簡體   English   中英

Perl script.file處理問題

[英]Perl script.file handling issues

我寫了一個Perl腳本:

#!/usr/bin/perl

use strict;
use warnings;

my $file_name;
my $ext = ".text";
my $subnetwork2;
my %files_list = ();
    opendir my $dir, "." or die "Cannot open directory: $!";
    my @files = readdir $dir;

sub create_files() {

    my $subnetwork;
    open(MYFILE, 'file.txt');
    while (<MYFILE>) {
        if (/.subnetwork/) {
            my @string = split /[:,\s]+/, $_;
            $subnetwork = $string[2];
        }
        if (/.set/ && (defined $subnetwork)) {
            my @string = split /[:,\s]+/, $_;
            my $file = $subnetwork . $string[1];
            open FILE, ">", "$file.text" or die $!;
            close(FILE);
        }
    }
    close(MYFILE);
}

sub create_hash() {
    foreach (@files) {
        if (/.text/) {

            open($files_list{$_}, ">>$_") || die("This file will not open!");

        }
    }
}

sub init() {
    open(MYFILE3, 'file.txt');
    while (<MYFILE3>) {
        if (/.subnetwork/) {
            my @string3 = split /[:,\s]+/, $_;
            $subnetwork2 = $string3[2];
            last;
        }
    }
    close(MYFILE3);
}

sub main_process() {

    init;
    create_files;
    create_hash;

    open(MYFILE1, 'file.txt');
    while (<MYFILE1>) {
        if (/.subnetwork/) {
            my @string3 = split /[:,\s]+/, $_;
            $subnetwork2 = $string3[2];
        }
        if (/.set/) {
            my @string2 = split /[:,\s]+/, $_;
            $file_name = $subnetwork2 . $string2[1] . $ext;
        }
        if (/.domain/ || /.end/ || ($. < 6)) {
            my $domain = $_;
            foreach (@files) {
                if (/.text/ && /$subnetwork2/) {
                    prnt { $files_list{$_} } "$domain";
                }
            }
        }
        elsif ($. >= 6) {
            print { $files_list{$file_name} } "$_";
        }
     }
    close(MYFILE1);
    foreach my $val (values %files_list) { close($val); }
    closedir $dir;
}

main_process;

該腳本根據file.txt的內容在當前目錄中創建文件,然后再次打開這些文件。

然后,它開始處理file.txt並根據動態設置的文件名重定向行。

文件名的此設置還基於文件file.txt的數據。

我在這里面臨的問題是重定向僅到單個文件。 這意味着文件句柄存在一些問題。

預期將創建的所有文件均已完美創建,但數據僅進入其中一個。

我懷疑重定向時使用的文件句柄是否存在問題。

誰能幫忙嗎? 示例輸入文件如下:

..cnai #Generated on Thu Aug 02 18:33:18 2012 by CNAI R21D06_EC01, user tcssrpi
..capabilities BASIC
.utctime 2012-08-02 13:03:18
.subnetwork ONRM_ROOT_MO:NETSim_BAG
.domain BSC
.set BAG01
AFRVAMOS="OFF"
AWBVAMOS="OFF"
ALPHA=0
AMRCSFR3MODE=1,3,4,7
AMRCSFR3THR=12,21,21
AMRCSFR3HYST=2,3,3
AMRCSFR3ICM=
AMRCSFR4ICM=
USERDATA=""
.set BAG02
AFRVAMOS="OFF"
AWBVAMOS="OFF"
ALPHA=0
AMRCSFR3MODE=1,3,4,7
AMRCSFR3THR=12,21,21
AMRCSFR3HYST=2,3,3
..end

我面臨的問題是在執行過程中:

> process.pl 
Use of uninitialized value in ref-to-glob cast at process.pl line 79, <MYFILE1> line 6.
Can't use string ("") as a symbol ref while "strict refs" in use at process.pl line 79, <MYFILE1> line 6.

我能理解的問題是此行:

print { $files_list{$_} } "$domain";

但是我不明白為什么!

我需要的輸出是:

> cat NETSim_BAGBAG01.text 
.set BAG01
AFRVAMOS="OFF"
AWBVAMOS="OFF"
ALPHA=0
AMRCSFR3MODE=1,3,4,7
AMRCSFR3THR=12,21,21
AMRCSFR3HYST=2,3,3
AMRCSFR3ICM=
AMRCSFR4ICM=
USERDATA=""


> cat NETSim_BAGBAG02.text
.set BAG02
AFRVAMOS="OFF"
AWBVAMOS="OFF"
ALPHA=0
AMRCSFR3MODE=1,3,4,7
AMRCSFR3THR=12,21,21
AMRCSFR3HYST=2,3,3
> 

您在以下幾行中遇到的問題:

open(PLOT,">>$_") || die("This file will not open!");
$files_list{$_}=*PLOT;

您應該將它們替換為:

open($files_list{$_},">>$_") || die("This file will not open!");

這部分代碼是關鍵:

 open(PLOT,">>$_") || die("This file will not open!");
 $files_list{$_}=*PLOT;

問題在於,您實際上是將文件句柄PLOT用作全局變量。 哈希中的每個條目都指向同一文件句柄。 替換為以下內容:

 local *PLOT;
 open(PLOT,">>$_") || die("This file will not open!");
 $files_list{$_}=*PLOT;

您已經非常迷戀這個程序。 不需要哈希表或多個子例程。

這是對代碼的快速重構,該代碼可處理數據並寫入文件NETSim_BAG.BAG01.textNETSim_BAG.BAG02.text 我在子網和集合之間放置一個點,以使名稱更清晰。

use strict;
use warnings;

my $out_fh;
open my $fh, '<', 'file.txt' or die $!;
my ($subnetwork, $set, $file);

while (<$fh>) {

    if ( /^\.subnetwork\s+\w+:(\w+)/ ) {
      $subnetwork = $1;
    }
    elsif ( /^\.set\s+(\w+)/ and $subnetwork) {
      $set = $1;
      $file = "$subnetwork.$set.text";
      open $out_fh, '>', $file or die qq(Unable to open "$file" for output: $!);
      print $out_fh;
    }
    elsif ( /^\.\.end/ ) {
      undef $subnetwork;
      undef $file;
    }

    if (/^[^.]/ and $file) {
      print $out_fh $_;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM