+ -
当前位置:首页 → 问答吧 → 一个for循环的问题

一个for循环的问题

时间:2010-07-30

来源:互联网

本帖最后由 东方补白 于 2010-07-30 12:31 编辑

正在看别人写的代码,有一段不太明白,请教一下大家。
下面一段代码的目的是处理从文本中读取出来的内容,如果在某行匹配了</rpc-reply>字符串,就删除其之后的内容。
但是如下的部分是不是多了一个'{'?
  1. for (my $j = $i +1; $j < 0; $j++) {
  2.             {
  3.              delete $outArray[$j];
  4.             }
  5.           $i = 0;
  6.           }
复制代码
还是说这种写法等同于如下代码,即$i=0是在for循环之外。
  1. for (my $j = $i +1; $j < 0; $j++) {
  2.              delete $outArray[$j];
  3.             }
  4.            $i = 0;
复制代码
完整的部分
  1. sub session_cmd {

  2.         my ($session, $command) = @_;

  3.         my $out = '';
  4.         my $err = '';
  5.         my $exit = '' ;
  6.         my @outArray;
  7.         my $i;
  8.         my $counter = 0;
  9.         my $nodata = 1;
  10.         my $rc;
  11.         my $foundCmd = 0;


  12.         $session->clear_accum();

  13.         print $session "$command\n";


  14.         $rc = $session->expect( $timeout, '-re', $prompt, '-re', 'vty\)#' );
  15.         if (!defined($rc)) {
  16.                 $err = "never got prompt from host!\n";
  17.                 undef $out ;
  18.                 $exit = "";
  19.                 print SINK "Timed out in $timeout seconds for $command\n";
  20.     #   print SINK "timed out on command: $command\n";
  21.                 print  "timed out on command: $command\n";
  22.         }
  23.         else
  24.         {

  25.                 $nodata = 0;
  26.      #  print SINK "Recieved response in $timeout seconds for $command\n";
  27.                 $out = $session->exp_before;

  28.                 @outArray = split( /\n/, $out);
  29.                 for ( $i = 0; $i < 5; $i++) {
  30.                         $foundCmd = 1 if ($outArray[$i] =~ /$command/);
  31.                         $foundCmd = 1 if ($outArray[$i] =~ /syntax\s+error/) ;
  32.                 }



  33.        if ( $command =~ /xml/i)
  34.         {

  35.          delete $outArray[0];
  36.          for ( $i = -5; $i < 0; $i++) {
  37.          if ($outArray[$i] =~ /<\/rpc-reply>/)
  38.           {
  39.             for (my $j = $i +1; $j < 0; $j++) {
  40.             {
  41.              delete $outArray[$j];
  42.             }  # end for
  43.           $i = 0;
  44.           }    # end for ( $i =

  45.          }   # end if ($outArray

  46.          }
  47.        }
  48.          $out = join("\n", @outArray);

  49.      }


  50. my $retryCounter = 0;
  51. while ( ($retryCounter < 5) && (0 == $foundCmd))
  52. {
  53. $rc = $session->expect( 1, '-re', $prompt, '-re', 'vty\)#' );
  54.   if (!defined($rc)) {
  55.       print "DEBUG: no residual data $retryCounter\n";
  56.      }
  57.   else
  58.     {
  59.        $nodata = 0;
  60.      #  print SINK "Recieved response in $timeout seconds for $command\n";

  61.       my $out1 = $session->exp_before;
  62.       my @out1Array = split( /\n/, $out1);
  63.        for ( $i = 0; $i < 5; $i++) {
  64.          $foundCmd = 1 if ($out1Array[$i] =~ /$command/);
  65.           $foundCmd = 1 if ($out1Array[$i] =~ /syntax\s+error/) ;
  66.         }

  67.        if ( $command =~ /xml/i)
  68.         {

  69.          delete $out1Array[0];
  70.          for ( $i = -5; $i < 0; $i++) {
  71.          if ($out1Array[$i] =~ /<\/rpc-reply>/)
  72.           {
  73.             for (my $j = $i +1; $j < 0; $j++) {
  74.             {
  75.              delete $out1Array[$j];
  76.             }   # end for $j
  77.           $i = 0;
  78.           }   #end for (my $j = $i +1; $j < 0; $j++) {

  79.          }   # end if ($out1Array[$i] =~ /<\/rpc-reply>/)

  80.          }    # end   for ( $i = -5; $i < 0; $i++) {




  81.         }  #end   if ( $command =~ /xml/i)
  82.        $out1 = join("\n", @out1Array);
  83.        $out = $out . "\n" . $out1;
  84.      }
  85.      $retryCounter = $retryCounter +1;
  86.   } # end while $retryCounter
  87. #  $out = $session->exp_before;
  88. #print SINK "Recieved response (unknown)in $counter seconds for $command\n";

  89.       if ($foundCmd)
  90.        {
  91.         print SINK "DEBUG: retryCounter: $retryCounter found command $command\n";
  92.         print  "DEBUG: retryCounter: $retryCounter found command $command\n";
  93.        }
  94.        else
  95.        {
  96.         print SINK "DEBUG: $prompt did not find command $command\n";
  97.         print  "DEBUG: $prompt did not find command $command\n";
  98.        }



  99.         $session->clear_accum();
  100.         return ($out, $err, $exit);
  101. }
复制代码
多谢各位了。

作者: 东方补白   发布时间: 2010-07-30

观望 学习

作者: OnlyForStudy   发布时间: 2010-07-30