gpt4 book ai didi

arrays - 在 CSV 中写入数组值

转载 作者:行者123 更新时间:2023-12-02 07:02:31 26 4
gpt4 key购买 nike

我有一个元素数组,我需要使用 Perl 文件处理将它们存储在单独的单元格中。

foreach(@type)
{
print FH qq|"@type","@date","@loc","@title"\n|;
}

我使用上面的代码,但它只存储数组中的最后一个元素。

提前致谢。

最佳答案

如果使用foreach(@type),特殊变量$_会被一一赋值@type。在您的代码中,您没有使用变量 $_。相反,您输出 @type,即您正在迭代的数组。你可能想遍历索引,比如

for (0 .. $#type) {
print FH qq("$type[$_]","$date[$_]","$loc[$_]","$title[$_]"\n);
}

或者,使用命名循环变量:

for my $index (0 .. $#type) {
print FH qq("$type[$index]","$date[$index]","$loc[$index]","$title[$index]"\n);
}

请注意,如果您的列可能包含换行符或双引号,您最好使用 Text::CSV .

关于arrays - 在 CSV 中写入数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18058292/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com