【发布时间】:2019-02-19 16:23:04
【问题描述】:
我有一个 CSV 文件,其中包含以下用逗号分隔的信息...
Owner,Running,Passing,Failing,Model
D42,21,54,543,Yes
T43,54,76,75,No
Y65,76,43,765,Yes
我想打开这个 CSV 文件并将其包含在我的程序中的 perl 哈希中。我也对在 has 中打印特定元素所需的代码感兴趣。例如,我将如何打印“所有者”Y65 的“通过”计数。
我目前拥有的代码:
$file = "path/to/file";
open $f, '<', $files, or die "cant open $file"
while (my $line = <$f>) {
#inside here I am trying to take the containments of this file and place it into a hash. I have tried numerous ways of trying this but none have seemed to work. I am leaving this blank because I do not want to bog down the visibility of my code for those who are kind enough to help and take a look. Thanks.
}
除了将 csv 文件放在散列中外,我还需要了解打印和浏览特定元素的语法。非常感谢您。
【问题讨论】:
-
是的,这很容易,但希望您展示您所尝试的内容。请发布您正在努力的代码。请参阅How to ask了解更多信息
-
@hakonhagland 我发布了一些我拥有的代码。我最初没有发布它,因为我不想在我的错误代码中阻碍任何人的视野。提前致谢。
-
@hakonhagland 谢谢你的帮助。我已经完全按照您告诉我的方法进行了尝试,但未能达到稳定的输出。您是否可以针对该问题编写一些示例代码以将我推向正确的方向?任何帮助是极大的赞赏。非常感谢
-
考虑使用CPAN模块Text::CSV::Hashify。