gpt4 book ai didi

perl - 在excel电子表格中打印文本

转载 作者:行者123 更新时间:2023-12-04 04:36:09 24 4
gpt4 key购买 nike

文本文件

malloc
calloc
free

.C 源文件

我的代码
my $word_search_path = $ENV{'SCRIPT_PATH'}."\\word_search.txt";
open(FILE1, "< $word_search_path") or die $!;
chomp(my @words = <FILE1>);
close(FILE1);

my $input = $ARGV[0];
open(FILE, $input) or die $!; #source file
my @lines = <FILE>;
close(FILE);

my $temp_path = $ENV{'TEMPLOG'}."\\dynamic_alloc_log.csv";
open(FILE2, "> $temp_path") or die "couldn't open the file!";

foreach my $word(@words){
my $count = 0;
foreach(@lines){
if($_ =~ $word){
$count++;
}
}
print FILE2 $word. $count."\n";
}
close(FILE2);

我得到的输出
malloc0       #in 1st field
calloc1 #in 2nd field
free2

我需要的输出
malloc    0      # malloc in one field , 0 in next field
calloc 1
free 2

如果我可以不用任何 Perl 模块,那就太好了。另一件事是文本文件数据是用户定义的。所以它不是固定的。

最佳答案

尝试将您的打印语句更改为

print FILE2 "${word},${count}\n";

或者
printf FILE2 "%s,%s\n", $word, $count;

由于您已经将输出存储到 .csv文件,它应该在 MS Excel 中正确显示字段

关于perl - 在excel电子表格中打印文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19725962/

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