home *** CD-ROM | disk | FTP | other *** search
/ c't freeware shareware 1997 / CT_SW_97.ISO / pc / software / entwickl / win95 / plsei306.exe / aspSamples / loops.asp < prev    next >
Text File  |  1997-03-22  |  4KB  |  127 lines

  1. <%@ LANGUAGE = PerlScript%>
  2. <HTML>
  3. <HEAD>
  4. <!-- 
  5.     Copyright (c) 1996, Microsoft Corporation.  All rights reserved.
  6.     Developed by ActiveWare Internet Corp., http://www.ActiveWare.com
  7. -->
  8.  
  9. <!-- the following script is from the "Writing ActiveX Server Scripts"
  10.      section of the "ActiveX Server Scripting Guide." -->
  11. <TITLE> Testing loops in PerlScript at different conditions </TITLE>
  12. </HEAD>
  13.  
  14. <BODY> <BODY BGCOLOR=#FFFFFF>
  15. <!-- 
  16.     ActiveWare PerlScript sample 
  17.     PerlScript:  The coolest way to program custom web solutions. 
  18. -->
  19.  
  20. <!-- Masthead -->
  21. <TABLE CELLPADDING=3 BORDER=0 CELLSPACING=0>
  22. <TR VALIGN=TOP ><TD WIDTH=400>
  23. <A NAME="TOP"><IMG SRC="PSBWlogo.gif" WIDTH=400 HEIGHT=48 ALT="ActiveWare PerlScript" BORDER=0></A><P>
  24. </TD></TR></TABLE>
  25.  
  26. <H3> Repeating a Loop While a Condition Is True</H3><P>
  27.  
  28. Use the While keyword to check a condition in a Do...Loop statement. You can check the condition before you enter the loop (as shown in the first example following this paragraph), or you can check it after the loop has run at least once (as shown in the second example).<P>
  29.  
  30. <%
  31.   #First example of the While keyword
  32.  
  33.   $MyNum = 20;
  34.   
  35.   $Counter = 0;
  36.   
  37.   while ($MyNum >10)
  38.   {
  39.     $MyNum--;
  40.     $Counter++;
  41.    }; 
  42. %>
  43. The first loop made <%= $Counter %> repetitions.<P>
  44. <%
  45.  
  46.   $Counter = 0;
  47.   $MyNum = 9;
  48.  
  49.  $Counter++;
  50.  $MyNum--;
  51.  while (MyNum > 10) 
  52.   {
  53.     $MyNum--;
  54.     $Counter++;
  55.    };
  56. %>
  57.  
  58. The second loop made <%= $Counter %> repetitions.<P>
  59.  
  60. <H3>Repeating a Statement Until a Condition Becomes True</H3>
  61. <P> 
  62. You can use the Until keyword in two ways to check a condition in a Do...Loop statement. You can check the condition before you enter the loop (as shown in the first example following this paragraph), or you can check it after the loop has run at least once (as shown in the second example). As long as the condition is False, the looping occurs.
  63. <P>
  64. <%
  65.   #First example of the Until keyword
  66.   $Counter = 0;
  67.   $MyNum = 20;
  68.  
  69.  while ($MyNum != 10)
  70.     {
  71.       $MyNum--; 
  72.     $Counter++;
  73.     };
  74. %>
  75. The first loop made <%= $Counter %> repetitions.<P>
  76.  
  77. <%
  78.   # Second example of the Until keyword
  79.   $Counter = 0;
  80.   $MyNum = 1;
  81.   while ($MyNum!=10)
  82.   {
  83.     $MyNum++;
  84.     $Counter++;
  85.    };
  86.   %>
  87. The second loop made <%= $Counter %> repetitions.<P>
  88. <P>
  89.  
  90.  
  91. <H3>Exiting a Do ... Loop Statement from Inside the Loop</H3>
  92. <P>
  93. You can exit a Do ... Loop by using the Exit Do statement. You usually want to exit when you have accomplished the task the loop is performing or in certain situations to avoid an endless loop.<P>
  94.  
  95. In the following example, myNum is assigned a value that creates an endless loop. The If...Then...Else statement checks for this condition, preventing the endless repetition.
  96. <P>
  97.  
  98. <%
  99.   $Counter = 0;
  100.   $MyNum = 9;
  101.   while ($MyNum != 10)
  102.   {
  103.     $MyNum--;
  104.     $Counter++;
  105.     if ($MyNum < 10) 
  106.     {
  107.     $MyNum=10;
  108.     }
  109.  };
  110. %>
  111. The loop made <%= $Counter %> repetitions.<P>
  112. <!-- +++++++++++++++++++++++++++++++++++++
  113. here is the standard showsource link - 
  114.     Note that PerlScript must be the default language --> <hr>
  115. <%
  116.     $url = $Request->ServerVariables('PATH_INFO')->item;
  117.     $_ = $Request->ServerVariables('PATH_TRANSLATED')->item;
  118.     s/[\/\\](\w*\.asp\Z)//m;
  119.     $params = 'filename='."$1".'&URL='."$url";
  120.     $params =~ s#([^a-zA-Z0-9&_.:%/-\\]{1})#uc '%' . unpack('H2', $1)#eg;
  121. %>
  122. <A HREF="index.htm"> Return </A>
  123. <A HREF="showsource.asp?<%=$params%>">
  124. <h4><i>view the source</i></h4></A>  
  125.  
  126. </BODY>
  127. </HTML>