从哈希perl写入CSV文件 [英] Write to a CSV file from a hash perl

查看:665
本文介绍了从哈希perl写入CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,目前从 FILE 1 读取,看起来像下面的一个,并匹配某些字符。例如

I have a program that at the moment reads from FILE 1 looking like the one below and matching certain characters. e.g

Type, Fruit, Description, quantity
tropical, banana, tasty and yummy, 5
tropical, grapefruit, bitter and not yummy, 2
... and so on

首先我想为每个'Type'创建哈希哈希, ,'Fruit','Description','Quantity',并将不同的值存储在引用散列中。下面的代码可以正常工作。

First of all I wanted to create hash of hashes for each 'Type', 'Fruit', 'Description', 'Quantity' and store the different values in the reference hashes. That works fine with the code below.

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),这将是一个表,其中包含每个散列散列('Type','Fruit','Description','Quantity')的列。 $ b

So my question is How can I put this data(data is not fixed) into a csv file or anything related(maybe xls), that would be a table with columns for each hash of hashes ('Type', 'Fruit', 'Description', 'Quantity').

推荐答案

我同意哈希的哈希是一件好事,但我认为你不是以一种你可以轻松检索的方式存储它。

I agree that hashes of hashes is a good thing, but I think you're not storing it in a way you can retrieve it easily.

一种方法就是这样。

{ id_1 => {
             data_1 => "blah",
             data_2 => "foo",
             ...
           },
  id_2 => {
             ...
           },
  ...
 }

首先,您需要选择哪个列将是ID。这将确定每个ROW的唯一性。让我们说你的例子让我们选择水果,因为我们假设没有两个水果出现在同一个文件。所以我们会有这样的:

First of all, you need to pick which column would be the "ID". This would determine the uniqueness of each ROW. Let's say for your example let's pick the fruit, since we're assuming there would be no two fruits that would appear in the same file. So we would have something like this:

{ banana => {
             type => "tropical",
             description => "tasty and yummy",
             ...
           },
  grapefruit => {
             ...
           },
  ...
 }

为了将它改回CSV,我们循环遍历哈希。

In order to change this back to CSV, we loop through the hashes.

my %fruit_data; #let's assume that this already has the data in it

foreach my $fruit ( keys %fruit_data ) { 

    #given the $fruit you can now access all the data you need
    my $type = %fruit_data{$fruit}{'type'};
    my $desc = %fruit_data{$fruit}{'description'};
    # etc...

    # then you may want to store them in a scalar in any order you want
    my $row = "$field,$type,$desc etc.\n";

    # then work your way from there

}

这篇关于从哈希perl写入CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆