我有一个目前从 FILE 1 中读取内容并匹配特定字符的程序,例如下面的样子:
Type, Fruit, Description, quantity tropical, banana, tasty and yummy, 5 tropical, grapefruit, bitter and not yummy, 2 ...以此类推首先,我想为每个“Type”、“Fruit”、“Description”和“Quantity”创建哈希表,并将不同的值存储在参考哈希表中。使用下面的代码可以很好地实现这一点。
use strict;
use warnings;
use Data::Dumper;
use Text::CSV;
my %MacroA = ('Type' => {}, 'Fruit' => {}, 'Description' => {}, 'Quantity' => {});
open (my $file, '<', 'FRUITIES.txt') or die $!;
while (my $line = <$file>) {
if ($line =~ /\b(tropical)\b,/) {
$MacroA{Type}->{$1}++;
}
if ($line =~ /,\b(banana|grapefruit)\b,/) {
$MacroA{Fruit}->{$1}++;
}
if ($line =~ /,([\w\s]+?),/) {
$MacroA{Description}->{$1}++;
}
if ($line =~ /,([\d]+?)/) {
$MacroA{Quantity}->{$1}++;
}
}
close $file;
我的问题是如何将这些数据(数据不固定)放入CSV文件或任何相关的文件(可能是XLS),使其成为一个带有每个哈希散列列的表格(“类型”,“水果”,“描述”,“数量”)。