我对perl很新。我想从一个csv文件的第一列中获取字符串,并想要检查另一个文件中该单词的频率,并希望在第三个文件中打印输出。这是我的代码 -
#!/usr/bin/perl
$inputfile = 'Input.txt';
$outputfile = 'Out.csv';
$file = 'File.csv';
open(INPUT, "<$inputfile") or die "Could not read from $inputfile, program halting.";
open(OUTPUT, ">$outputfile") or die "Could not open $outputfile, program halting.";
open(FILE, "<$file") or die "Could not read from $file, program halting";
@temp;
@token;
$count;
#skip first line of approved file
if(<FILE>)
{
(@temp) = split (/\,/);
}
$count = 0;
while(<FILE>)
{
@temp = split (/\,/);
print "First Column - @temp[0], ";
print "Count - @temp[1], ";
print "Priority - @temp[3], ";
$count = 0;
while(<INPUT>)
{
#read the fields in the current record into an array
@words = split(/\s+/);
foreach $word (@words)
{
$temp1 = @temp[0];
if($word == $temp1)
{
$count++;
}
}
}
print "$temp1 - Count - $count \n ";
print OUTPUT "$temp1,$count,@temp[3]";
print OUTPUT "\n";
}
close INPUT;
close OUTPUT;
close FILE;
print "Done, please check the output file.\n";
有人请帮忙。
答案 0 :(得分:0)
首要的事情始终是
use strict;
use warnings;
您误解了将数组中的值作为标量获取。要从数组中获取标量值,请使用以下命令:
@temp = split (/\,/); #this is array
print "First Column - $temp[0], "; #temp[[0] is scalar value of array
print "Count - $temp[1], "; #temp[[1] is scalar value of array
print "Priority - $temp[3], "; #temp[[2] is scalar value of array
在这里
foreach $word (@words)
{
$temp1 = $temp[0]; #this should be scalar.
if($word == $temp1)
{
$count++;
}
}
无论您在哪里使用@temp[0],@temp[1]
..都是错误的用法,它应该是$temp[0],$temp[1],$temo[2]
....