user690182
user690182

Reputation: 279

Tcl : why the variable does not get increment?

What I would like to ask you is, why the following snippet does not increment the variable $times ?

#!/usr/bin/tclsh 

set logging { 
LC/0/1/CPU0:Jan 27 08:24:17.014 CET: ifmgr[175]: %PKT_INFRA-LINK-3-UPDOWN : Interface GigabitEthernet0/1/0/33, changed state to Down 
LC/0/1/CPU0:Jan 27 08:24:17.014 CET: ifmgr[175]: %PKT_INFRA-LINEPROTO-5-UPDOWN : Line protocol on Interface GigabitEthernet0/1/0/33, changed state to Down 
}
set times 0
set records [ split $logging "\n" ] 
foreach rec $records { 
    puts $rec
    incr $times 1
    puts $times       ;# puts [ incr $times 1 ]
} 

Many thanks.

Upvotes: 0

Views: 3013

Answers (2)

Donal Fellows
Donal Fellows

Reputation: 137627

In Tcl, the name of the variable is times; $times means “read times now and use the value read from it”. When you're wanting to increment a variable, you need to tell the increment command the name of the variable to adjust, i.e., you need to pass times and not the contents of the times variable (which is what $times does, which works out to passing 0 in; a legal variable name but probably not what you wanted; putting variable names in variables is not normally recommended because it tends to make programmers' heads ache).

Thus, instead of incr $times 1 you want:

incr times

(The 1 is optional; it's the default amount to increment by.)

Upvotes: 5

TrojanName
TrojanName

Reputation: 5365

It should be

incr times 1 

or simply

incr times  

Upvotes: 8

Related Questions