codingdir logo sitemap sitemap |

"can't read:variable is array" error in ns2

By : , Category : tcl

In Tcl, $varName means “read from the variable called varName” and is not a general reference to the variable (unlike some other languages, notably Perl and PHP, which do rather different things). Reading from a whole array, instead of an element of that array, is always an error in Tcl.

To pass an array to a command, you pass the name of that array in. It's then up to that command to access it as it sees fit. For procedures and methods written in Tcl, it'll typically involve upvar to link the array into a local view. (Things written directly in C or C++ have far fewer restrictions as they don't automatically push a Tcl stack frame.)

Note however that the command must be expecting the name of an array when you pass that name in. (Good programmers will document this fact, of course.) Whether create-god does, I really have no idea; it's not a general Tcl command but rather something that's more specific. (Part of ns2? Or maybe your own code.)

Example of passing in an array

An example of passing in an array by name is the parray command that should be part of every Tcl distribution. It's a procedure that prints an array out. Here's the source code without a few boiler-plate comments:

proc parray {a {pattern *}} {
    upvar 1 $a array
    if {![array exists array]} {
        error ""$a" isn't an array"
    set maxl 0
    set names [lsort [array names array $pattern]]
    foreach name $names {
        if {[string length $name] > $maxl} {
            set maxl [string length $name]
    set maxl [expr {$maxl + [string length $a] + 2}]
    foreach name $names {
        set nameString [format %s(%s) $a $name]
        puts stdout [format "%-*s = %s" $maxl $nameString $array($name)]

The key thing here is that we first see upvar 1 to bind the named variable in the caller to a local variable, and a test with array exists to see if the user really passed in an array (so as to give a good error message rather than a rubbishy one). Everything else then is just the implementation of how to actually pretty-print an associative array (finding out the max key length and doing some formatted output); it's just plain Tcl code.

ReLated :

You could use mkdir to create folders and subfolders recursively before writing the files:

mkdir($path, 0777, true);

Note the third parameter, which means recursive

See the mkdir docs:

put in my.cnf

ft_min_word_len = 3

innodb_lock_wait_timeout variable in MySQL 5.1 is not dynamic, so you must specify it on command line when starting server:

 mysqld --innodb_lock_wait_timeout=100

or in the configuration file (my.cnf, my.ini):


restart MySQL server after configuration file change.

Your loop statement is the problem because of missing encolsing {}:

for(i=0;i<cities;++i)      // loop
    scanf("%d",&arr[i]);   // but there is no enclosing braces. so this is the only statement that loops
arr2[i]=0;              //<<<<<< this is executed when loop is finished, i.e i==cities 

In other words, you assign arr2[cities], which is out of bouds as it's indexed from 0 to cities-1. This causes the segfault.

Looking at the rest of the code, I guess you inteded to to:

for(i=0;i<cities;++i) {    // loop, but for the block 

Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © All Rights Reserved .