2

This is a bizarre issue that I can't seem to figure out. I am using TCL 8.5 and I am trying read data from a CSV file into matrix using the csv::read2matrix command. However, every time I do it, it says the matrix I am trying to write to is an invalid command. Snippet of what I am doing:

package require csv
package require struct::matrix

namespace eval ::iostandards {
    namespace export *
}

proc iostandards::parse_stds { io_csv } {
    # Create matrix
    puts "Creating matrix..."
    struct::matrix iostdm

    # Add columns
    puts "Adding columns to matrix..."
    iostdm add columns 6

    # Open File
    set fid [open $io_csv r]
    puts $fid

    # Read CSV to matrix
    puts "Reading data into matrix..."
    csv::read2matrix $fid iostdm {,}

    close $fid
}

When I run this code in a TCLSH, I get this error:

invalid command name "iostdm"

As far as I can tell, my code is correct (when I don't put it in a namespace. I tried the namespace import ::csv::* ::struct::matrix::* and it didn't do anything.

Is there something I am missing with these packages? Nothing on the wiki.tcl.tk website mentions anything of the sort, and all man packages for packages don't mention anything about being called within another namespace.

Roman Kaganovich
  • 618
  • 2
  • 6
  • 27
Blitztm
  • 23
  • 2

2 Answers2

2

The problem is iostdm is defined inside the iostandards namespace. That means, it should be referenced as iostandards::iostdm, and that is how you should pass to csv::read2matrix:

    csv::read2matrix $fid iostandards::iostdm {,}

Update

I noticed that you hard-coded adding 6 columns to the matrix before reading. A better way is to tell csv::read2matrix to expand the matrix automatically:

    csv::read2matrix $fid iostandards::iostdm , auto
Hai Vu
  • 37,849
  • 11
  • 66
  • 93
1

I want to add to Hai Vu's answer

From my testing, for commands such as csv::read2matrix and csv::write2matrix, if you have nested namespaces, it appears you have to go to the highest one.

I had a case where the structure was...

csv::read2matrix $fid ::highest::higher::high::medium::low::iostdm , auto
koala123
  • 13
  • 1
  • 5
  • 1
    Thanks for comment. This helps explains a lot in what I'm seeing with some embedded packaging. – Blitztm Sep 11 '13 at 16:54