在将csv数据写入命名空间中的矩阵时,TCL将抛出无效的命令名称 [英] TCL throws invalid command name when writing csv data to a matrix within a namespace

查看:297
本文介绍了在将csv数据写入命名空间中的矩阵时,TCL将抛出无效的命令名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个奇怪的问题,我似乎无法想像。我使用TCL 8.5,我正在尝试使用 csv :: read2matrix 命令将CSV文件中的数据读入矩阵。但是,每次我做,它说我试图写的矩阵是一个无效的命令。我正在做的代码段:

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
}

当我在TCLSH中运行此代码时,会收到以下错误:

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

invalid command name "iostdm"

据我所知,我的代码是正确的我不把它放在命名空间中,我试过命名空间import :: csv :: * :: struct :: matrix :: * ,它没有做任何事情。

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.

这些包有什么缺失吗? wiki.tcl.tk网站上没有提到任何类型的东西,所有的软件包包都没有提到在另一个命名空间中被调用。

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.

推荐答案

问题是 iostdm iostandards 命名空间中定义。这意味着,它应该被引用为 iostandards :: iostdm ,这就是你应该如何传递到 csv :: read2matrix

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 {,}



更新



我注意到你在阅读之前硬编码添加了6列到矩阵。更好的方法是让 csv :: read2matrix 自动展开矩阵:

    csv::read2matrix $fid iostandards::iostdm , auto

这篇关于在将csv数据写入命名空间中的矩阵时,TCL将抛出无效的命令名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆