title: expect inside while loop - loops forever
body: <p>I'm new to expect scripts, so please forgive my stumbling...</p>
  
  <p>Below is the meat of my expect script.  The intent is to scroll thru several screens of output, after each of which the user is prompted with "Continue? [y/n]".  </p>
  
  <p>Finally, when there are no more screens, a "% " prompt is displayed, which SHOULD cause execution to fall out of the while loop.</p>
  
  <pre><code>    set more_screens 1
      while {$more_screens &gt; 0} {  
          sleep 10
          expect {
              "\[y/n]" { send "y\r"}
              "% "  { set more_screens 0 }
          }
       }
  </code></pre>
  
  <p>What happens instead is...  it stays in the while loop forever, sending "y" over and over and over again.  I have set "exp_internal 1", and from that output it "seems" like the expect keeps re-reading text that it has already matched on, and so keeps seeing "[y/n]", and keeps sending "y" when, in fact, there are only 2 screens of output, and thus only 2 "Continue? [y/n]" prompts.</p>
  
  <p>(The sleep statement is probably not necessary - i just added it to maybe solve the problem - it did NOT - and to allow me to digest the debug output better.)</p>
  
  <p>Bottom line...  Are there any obvious blunders in my code?  I'll take any suggestions at improving this and eliminating the endless looping.</p>
  
  <p>EDIT BELOW added to this question, after James made a helpful suggestion.</p>
  
  <p>Thanks James for your quick response, and your helpful suggestion! But...</p>
  
  <p>The same problem persists with your approach (although, yours IS much more elegant, and I'll add this to my expect tool kit.)</p>
  
  <p>The problem sure seems to be, as noted initially, each execution of the expect statement RE-READS TEXT THAT WAS ALREADY READ AND COMPARED. Below is output when I execute the "exp_continue" code from James, and I set "exp_internal 1" to get debug output on my screen...</p>
  
  <pre><code>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  &gt;expect: does "get dump tables\r\n\r% get dump tables\n\r\r\nIfn  TableName           Configured &gt;MaxUse    InUse     LastDropTime\r\n3    cdm_app             100002     190       33        &gt;\r\n3    cdm_conv            2000002    675180    4813      \r\n3    cdm_pdisc           250002     &gt;250002    1304      01-24-2014-19:14:59\r\n3    cdm_kpi             100001     141       25        &gt;\r\n3    cdm_qoe             500003     204918    1578      \r\n3    cdm_qoe_hd          2500003    &gt;582993    1578      \r\n3    cdm_kpi_error_app   100001     5         2         \r\n3    &gt;cdm_kpi_error       100001     7         2         \r\n3    asr_cache           1000000    &gt;1000000   999995    \r\n3    asr_sess            2000000    62670     29748     \r\n3    &gt;asr_conn            3000000    64428     31147     \r\n3    asr_sess_keys       1500000    &gt;1015269   1009049   \r\n3    asr_conn_opts       6000000    0         0         \r\n3    &gt;asr_events          4000000    5239      144       \r\n3    skt_table           2000000    &gt;2000000   2000000   \r\n3    skt_trans           1000000    408020    254674    \r\n3    &gt;ses_sip_db          5000       0         0         \r\n3    ses_gtp_mob_txn     5000       &gt;0         0         \r\nContinue? [y/n]:  " (spawn_id exp6) match glob pattern "[y/n]"? yes
  &gt;expect: set expect_out(0,string) "n"
  &gt;expect: set expect_out(spawn_id) "exp6"
  &gt;expect: set expect_out(buffer) "get dump tables\r\n\r% get dump tables\n\r\r\nIfn"
  &gt;send: sending "y\r" to { exp6 }
  &gt;expect: continuing expect
  &gt;
  &gt;
  &gt;expect: does "  TableName           Configured MaxUse    InUse     LastDropTime\r\n3    &gt;cdm_app             100002     190       33        \r\n3    cdm_conv            2000002    &gt;675180    4813      \r\n3    cdm_pdisc           250002     250002    1304      01-24-2014-&gt;\r\n3    cdm_kpi             100001     141       25        \r\n3    cdm_qoe             500003     &gt;204918    1578      \r\n3    cdm_qoe_hd          2500003    582993    1578      \r\n3    &gt;cdm_kpi_error_app   100001     5         2         \r\n3    cdm_kpi_error       100001     &gt;7         2         \r\n3    asr_cache           1000000    1000000   999995    \r\n3    &gt;asr_sess            2000000    62670     29748     \r\n3    asr_conn            3000000    &gt;64428     31147     \r\n3    asr_sess_keys       1500000    1015269   1009049   \r\n3    &gt;asr_conn_opts       6000000    0         0         \r\n3    asr_events          4000000    &gt;5239      144       \r\n3    skt_table           2000000    2000000   2000000   \r\n3    &gt;skt_trans           1000000    408020    254674    \r\n3    ses_sip_db          5000       &gt;0         0         \r\n3    ses_gtp_mob_txn     5000       0         0         \r\nContinue? 
  &gt;[y/n]:  " (spawn_id exp6) match glob pattern "[y/n]"? yes
  &gt;expect: set expect_out(0,string) "n"
  &gt;expect: set expect_out(spawn_id) "exp6"
  &gt;expect: set expect_out(buffer) "  TableName           Con"
  &gt;send: sending "y\r" to { exp6 }
  &gt;^Csighandler: handling signal(2)
  &gt;async event handler: Tcl_Eval(exit 130)
  
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  </code></pre>
  
  <p>After the expect matches on "[y/n]" the first time, it then does a "expect: continuing expect" (middle of the above text output), then the block of text it reads next is, except for the first few words, THE SAME TEXT BLOCK IT ALREADY READ AND COMPARED.</p>
  
  <p>Am I missing anything??? This has to be a problem if the expect statements re-reads already processed output, yeah? (I have looked at the actual output sent by the target system, and it does NOT send out the same text block a second time.)</p>
  
  <p>Again, I'm new at expect scripts, but I can't see any other explanation for what the debug output above shows. (And, I apologize for having trouble formatting the output correctly - I really am trying!)</p>
  
  <p>Thanxks to anyone who has the patience to read all of the above, and perhaps has an explanation or suggestion.</p>