值未存储在文件<perl> </perl>中

时间:2013-10-07 19:05:09

标签: perl

我是perl的新手。我的动机是从txt文件中读取一些值并在我的perl脚本中使用它。

文本文件类似于::

Servers ::              
   (local)

      Tomas-Server1

      Tomas-Server2
      .........**

我需要做的是获取第3行值(Tomas-Server2)并在我的perl脚本中使用它。基本上它调用perl脚本的第3个值。

我为它写了一个基本代码::

my($ServName1,$ServName,$ServName3) = getservername($servername);
my ($filenam) = 'data.txt';
my @Param = ();

open(INFILE,"<$filenam") or die "Couldn't open $filenam for reading\n";
while(<INFILE>) {
    chop($_); 
    push(@Param,$_);
}
close(INFILE);
return @Param;
}

但是当我尝试使用“$ ServName2”值时,它不会返回任何内容。我想包含的值应该是“(本地)”。

2 个答案:

答案 0 :(得分:1)

您永远不会定义$ServName2变量。 $ServName1$ServName3之间的变量名为$ServName,没有数字。

答案 1 :(得分:1)

您的示例数据存在一些问题,但我想我明白您要做的是什么。假设您想要第三个服务器值(而不仅仅是第3行),您可能希望代码看起来像这样......

#!perl

#some includes
use strict;
use warnings;
use Data::Dumper;

#set some vars
my $TargetFileName="data_file.txt";
my $TargetServerNumber=3;

#call the sub
my @ServerArray=GetServerName($TargetFileName);

#did we get anything back?
if(@ServerArray) {
    #yep, we got some content
    print "Server number ".$TargetServerNumber."/".($#ServerArray+1)." is \"".$ServerArray[$TargetServerNumber-1]."\"\n"; #array numbers are 0-based
} else {
    #nope, we read the file but didn't any matching content
    print "No servers found in file \"".$TargetFileName."\".\n";
} #end if

print "\n";
print "Here's what was loaded into \@ServerArray...\n";
print Data::Dumper::Dumper(@ServerArray); #so you can see the full content of @ServerArray
print "All Done\n";
exit;

#----- subs go here -----

sub GetServerName {
    my $DataFileName=shift; #pull in the first arg - this alters @_ for the rest of the sub, so take care when doing this
    my @ReturnArray;
    #do some QA
    if(!$DataFileName) {die "You need to provide a file name to use this sub.\n"} #do we have a file name?
    if(!stat($DataFileName)) {die "The requested file name of \"".$DataFileName."\" does not exist.\n";} #does the file exist?
    open(INFILE, "<".$DataFileName) or die "Unable to open \"".$DataFileName."\" - ".$!;

    #ok, read the file content
    while(my $Line=<INFILE>) {
        chop($Line);
        $Line=~s/^\s+//g; #remove leading white spaces
        $Line=~s/\s+$//g; #remove trailing white spaces
        if(!$Line) {next;} #blank line, skip it

        #check for the headers
        if($Line=~m/^Servers/) {next;} #skip lines beginning with "Servers"
        if($Line=~m/^\(local\)/) {next;} #skip lines beginning with "(local)"

        #if we get here, we must want the line content, so add it to the array
        push(@ReturnArray, $Line);
    } #end while
    close(INFILE);

    #send the array data back, if any
    return @ReturnArray
} #end GetServerName sub

__END__

stuff below here does not need to be commented

我承认这不是解决问题的最佳方式,但是像大多数Perl黑客一样,它可以工作。您会注意到代码有点矫枉过正,并在将数据传递给某些操作之前进行验证检查 - 养成以您使用的任何语言执行此操作的习惯。这将帮助您在出现问题时返回更有意义的错误还将捕获与数据相关的问题。示例代码还会进行一些数据清理,因为前导和尾随可能会在以后引起您的问题。空白行(清洁后)也被删除。

BTW,我用作例子的数据文件看起来像这样......

Servers ::              
   (local)

      Tomas-Server1 
      Tomas-Server2  

      Tomas-Server3
      Tomas-Server4

      Tomas-Server5

      Tomas-ServerLast