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

标签 perl

文本文件

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 模块,那就太好了。还有一件事 TEXT FILE 数据是用户定义的。所以它不是固定的。

最佳答案

尝试将打印语句更改为

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

或者

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

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

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

相关文章:

珀尔 : Counting Duplicates

Perl 三元条件运算符

Perl:不推荐使用哈希作为引用

perl - 想要在 Perl 中通过 IPv6 链接本地 IP 发送 UDP 数据包

MySQL - PERL - 按多列排序,然后按列零件号字段排序

multithreading - 控制 Fork super 队列的长度

java - Perl/Java 字节编码差异

Perl 函数原型(prototype)

regex - 将周围的单词与匹配项一起提取

sql - 在Perl中使用DBI在一行中更新多个字段?