Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2006-05-18
15:06 • Ticket [1490503fff] regexp within foreach loop matches wrong status still Cl... artifact: ed10dfe592 user: dkf
14:57 • Ticket [1490503fff]: 4 changes artifact: edd7e54bea user: nobody
03:59 • Closed ticket [1490503fff]. artifact: 86e42145a1 user: msofer
2006-05-17
20:30 • New ticket [1490503fff]. artifact: dbbf977409 user: sachulka

regexp within foreach loop matches wrong

Closed / Invalid1490503fffffffffffffffffffffffffffffffffCreated 21 years agoUpdated 21 years ago
Descriptionplain2006-05-17 20:30:07
I recently upgraded from Tcl8.3 to Tcl8.4.13 on 
solaris and discovered this difference which does not 
seem right. 

here is my little program:

proc send_user { x } { puts $x }
set alarmRecord [list "abc: def" "ALARM 1: blah" "ghi: 
jkl" "ALARM 2: fjdasl"]
foreach record $alarmRecord {
   if [regexp {(ALARM)[^:]+([0-9]+)} $record match key 
value] {
      send_user ">KEY $key VALUE $value\n"
   } elseif [regexp {([^:]+):(.*)} $record match key 
value]  {
      send_user "KEY2 $key VALUE $value\n"
   }
} 

Here are the results, the key and value are different 
for different Tcl versions:
sapphire:~/thuyscripts {ewuthle_eusbd00250}> tclsh8.3
KEY2 abc VALUE  def
 
>KEY ALARM VALUE 1
 
KEY2 ghi VALUE  jkl
 
>KEY ALARM VALUE 2

sapphire:~/thuyscripts {ewuthle_eusbd00250}> tclsh
% info tclversion
8.4
KEY2 abc VALUE  def
 
>KEY ALARM 1 VALUE  blah
 
KEY2 ghi VALUE  jkl
 
>KEY ALARM 2 VALUE  fjdasl

So if regexp is in the foreach loop and the same 
variables (key/value) are used inside if - regexp does 
not match correctly.
msoferplain2006-05-18 03:59:37
Logged In: YES 
user_id=148712

The bug is actually in Tcl8.3 (although I did not confirm
the behaviour there), the behaviour in 8.4 is correct.

Your script is missing some braces in order to perform
according to your expectation: it should be

  if {[regexp ...]} {
      send_user ">KEY $key VALUE $value\n"
  } elseif {[regexp ...] {
      send_user "KEY2 $key VALUE $value\n"
  }

Please note that the conditions to 'if ... elseif' are
enclosed in braces. This means that they are passed to 'if'
as strings; when 'if' evals the first condition and finds it
is true, it never evaluates the second.

In your script as posted, both regexp's are evaluated
*before* they are passed as arguments to 'if'; so, even when
the first regexp matches, the values $key and $value are set
by the second regexp.
nobodyplain2006-05-18 14:57:49
Logged In: NO 

See also bug 219166, it was fixed in 8.4a4
dkf Ticket [1490503fff]: 1 change [History diff]2006-05-18 15:06:22
Log in (anonymous is fine) to add a comment.