Perl从文件中检索行

时间:2013-12-05 22:52:14

标签: perl cgi

我正在尝试在登录的用户名和与帐户文件中的任何一行匹配的下一行之间返回文件中的行。

更新:已完成

sub getUserComments()
{

open my $COMMENTS, '<', "comments.txt" or die "$!";
my $username = shift;
my @usercomments;

my @lines = <$COMMENTS>;
my $started = 0;
for my $line (@lines) 
{
   chomp($line);
   if($line eq $username)
    {
      $started = 1;  
     }

     elsif($started == 1)
     {
     #make accounts array
      my @accounts;
      open my $ACCOUNTS, '<', "accounts.txt" or die "$!";
      my @linesAccounts = <$ACCOUNTS>;
      for my $lineaccounts (@linesAccounts) 
      {
      chomp($lineaccounts);
        push(@accounts, $lineaccounts); #Fill the accounts array
      }

     if(grep {$_  eq $line} @accounts) #if the line matches any account
     {
      $started = 0; #set to 0 so entire loop will stop
     }
     else
     {

     my $comment = "\n$line";
     chomp($comment);

     push(@usercomments, $comment); #add line to comments array
     }

     }






}
return @usercomments;

}

几乎就在那里,现在它读取评论文件中用户名部分下面的所有内容,我只需要在它到达另一个用户名时停止阅读。

以下是调用方法的地方:

  my @usercomments = getUserComments($username);
  print textarea( -name    => "CommentArea",
                  -default => "@usercomments",
                  -rows    => 10,
                  -columns => 60 );

0 个答案:

没有答案