title: ns: Avg: can't use non-numeric string as operand of "+"
body: <p>This a code to simulate an MM1 queue server (not really an important fact). I'm trying to get the average value to the number of packets in the queue for each run. Given that I read the value of lambda from a file, modify it, save the new modified value of it in the file again, and run the simulation with current value of lambda:</p>
  
  <pre><code>#Create a simulator object
  set ns [new Simulator]
  
  global ns tracefile namf Qsize Qbw Qlost Qmon Avgvals InterArrivalTime lambda
  
     proc readfile {filename} {
      set f [open $filename]
      set data [read $f]
      close $f
      return $data
  }
  proc writefile {filename data} {
      set f [open $filename w]
      puts -nonewline $f $data
      close $f
  }
  
  
  #Define different colors for data flow (for NAM)
  $ns color 1 Blue
  
  #Open the NAM trace file
  set namf [open QMM1.nam w]
  $ns namtrace-all $namf
  
  set Qsize [open Qsize.tr w]
  set Qbw   [open Qbw.tr   w]
  set Qlost [open Qlost.tr w]   
  set Avgvals [open Avgvals.tr a+]
  set tracefile [open out.tr w]
  $ns trace-all $tracefile
  
  
  #set lambda and Mu
  
  set mu     1100.0
  
  catch {set lambda [readfile LamdaValue.tr]}
  set lambda [expr {$lambda + 100.0}]
  writefile LamdaValue.tr $lambda
  
  
  #Create the Node to generate the traffic Queue and Server
  set n1 [$ns node]
  set n2 [$ns node]
  
  # Since packet sizes will be rounded to an integer
  # number of bytes, we should have large packets and
  # to have small rounding errors, and so we take large bandwidth
  
  #Create link between the two nodes
  set link [$ns simplex-link $n1 $n2 100kb 0ms DropTail]
  
  #Monitor the queue for link (n1-n2) for nam
  $ns simplex-link-op $n1 $n2 queuePos 0.5
  
  
  #set up a large Queue capacity (n1-n2)
  $ns queue-limit $n1 $n2 100000
  
  # generate random interarrival times and packet sizes
  
  #Set arrivals to be exponential
  set InterArrivalTime [new RandomVariable/Exponential]
  
  #Avg=1/lambda
  #$InterArrivalTime set avg_ [expr 1/$lambda]
  
  
  set pktSize [new RandomVariable/Exponential]
  $pktSize set avg_ [expr 100000.0/(8*$mu)]
  
  #Set src to use UDP
  set src [new Agent/UDP]
  $src set fid_ 1
  $ns attach-agent $n1 $src
  
  # queue monitoring, write statistics to queueStat
  set Qmon [$ns monitor-queue $n1 $n2 [open queueStat.out w] 0.1]
  $link queue-sample-timeout
  
  
  proc record {} {
  global ns Qmon Qsize Qbw Qlost n1 n2 Avgvals lambda
  set time 0.05
  set now [$ns now]
  
  # print in the file $Qsize the current queue size
  # print in the file $Qbw the current used bandwidth
  # print in the file $Qlost the loss rate
  
  $Qmon instvar parrivals_ pdepartures_ bdrops_ bdepartures_ pdrops_ 
  puts $Qsize "[expr $parrivals_-$pdepartures_-$pdrops_]"
  puts $Qbw   "$now [expr $bdepartures_*8/1024/$time]"
  set bdepartures_ 0                                
  puts $Qlost "$now [expr $pdrops_/$time]"
  $ns at [expr $now+$time] "record"
  }   
  
  
  proc finish {} {
      global ns tracefile namf Qsize Qbw Qlost Qmon
      $ns flush-trace 
      #Close the NAM trace file
      close $namf
      close $Qsize
      close $Qbw
      close $Qlost
      close $tracefile   
      #Execute NAM on the trace file
      #exec ./nam QMM1.nam &amp; 
      exit 0 
  } 
  
  proc sendpacket {} {
      global ns src InterArrivalTime pktSize 
      set time [$ns now]
      $ns at [expr $time + [$InterArrivalTime value]] "sendpacket"
      set bytes [expr round ([$pktSize value])]
      $src send $bytes
  }
  
  
  
  proc Avg {} {
  global ns Qmon Qsize Qbw Qlost n1 n2 Avgvals lambda
  
  set sum 0.0
  set c 1
  
  set fid [open Qsize.tr r]
    set txt [read $fid]
    close $fid
  
    foreach {x} $txt {
      set sum [expr {$sum + $x}]
      incr c 1
    }
  
  
   set avg [expr $sum/$c]
  puts $Avgvals "$lambda $avg"
  
  }
  
  set sink [new Agent/Null]
  $ns attach-agent $n2 $sink
  $ns connect $src $sink
  
  proc repeatSim {} {
          global lambda
          if {$lambda &lt; 1002} {
          exec ./ns QMM1.tcl &amp;
          }
   }
  
  
      #Avg=1/lambda
      $InterArrivalTime set avg_ [expr 1/$lambda]
      puts "Lambda = $lambda"
          puts "Mu = $mu"
      $ns at 0.0 "record"
      $ns at 0.0001 "sendpacket"
      $ns at 1000.0 "Avg"
      $ns at 1000.0 "repeatSim"
  
  #Run the simulation
  $ns run
  </code></pre>
  
  <p>The problem is that it does the first three self calls from :</p>
  
  <pre><code>proc repeatSim {} {
          global lambda
          if {$lambda &lt; 1002} {
          exec ./ns QMM1.tcl &amp;
          }
  }
  </code></pre>
  
  <p>Then it gives the error in the title.
  The full error statement is:</p>
  
  <pre><code>ns: Avg: can't use non-numeric string as operand of "+"
      while executing
   "expr {$sum + $x}"
      (procedure "Avg" line 12)
      invoked from within
  "Avg"
  </code></pre>
  
  <p>I have tried several manipulations in the code but the error still shows in the same way.
  Can you tell what the problem is?</p>