在数组的perl哈希中转换csv

时间:2013-06-25 18:50:20

标签: perl perl-module

我在使用Perl将以下csv片段转换为哈希时遇到问题。

emp_no,birth_date,first_name,last_name,gender,hire_date
10001,1953-09-02,Georgi,Facello,M,1986-06-26
10002,1964-06-02,Bezalel,Simmel,F,1985-11-21
10003,1959-12-03,Parto,Bamford,M,1986-08-28
10004,1954-05-01,Chirstian,Koblick,M,1986-12-01
10005,1955-01-21,Kyoichi,Maliniak,M,1989-09-12

哈希应该是这样的:

$employee = {
emp_no=>[10001,10002,10003,10004,10005],
birth_date=>[1953-09-02,1964-06-02,1959-12-03],
simarly for fistname , lastname and hire_date

}

我试过这样的

while(<FH>){


    @keys = split /,/,$_ if $.==1;  #for the first line 

       @row = split /,/,$_;

          push @hash{@keys},@row;

}

3 个答案:

答案 0 :(得分:2)

仅在由于某种原因无法使用时使用此选项 http://metacpan.org/pod/Text::CSV模块:)

my %employee;
while (<ARGV>)) {
  next if /^emp/;
  my @r = split/,/; 
  push @{$employee{$_}}, shift @r 
       for qw(emp_no birth_date first_name last_name gender hire_date);   
}

答案 1 :(得分:1)

类似的东西:

while ( my $line = readline($fh) ) {
    chomp $line;
    my ( $emp_no, $birth_date, $first_name, $last_name, $gender, $hire_date ) = split /,/, $line;
    push @{ $employee->{emp_no} }, $emp_no;
    #etc.
}

答案 2 :(得分:0)

Text :: CSV会将您的数据存储为散列数组,每个散列键都是列名。这似乎是最有意义的。例如:

my %employee =  %{ $employee_array[2] };  #Row #3 of your file:
print "The name of the third employee is $employee{first_name} $employee{last_name}\n";

因此,您的数组的单行包含该员工的所有数据。

在您的情况下,您必须在多个阵列中保持索引相同:

print "The name of the third employee is $first_name[2] $last_name[2]\n";

如果您有一个对员工进行操作的功能,则必须将所有阵列传递给该功能:

print_paycheck($first_name[1], $last_name[1], $employee_num[1], $hire_date[1]...);

如果你有一系列哈希,你可以这样做:

print_paycheck($employee_array[1]);

我认为你不知道参考文献。许多初学Perl书籍都没有讨论它们,它们也不是Perl的明显扩展。但是,引用允许您创建这些更复杂的数据结构。幸运的是,Perldoc有一个很棒的Tutorial。我建议你读一读。

实际上,您可能希望存储由员工编号键入的数据,因此您需要散列哈希值。

这是哈希哈希的一个例子。 注意:这是我执行程序的方式。首先,我会使用Text::CSV(如果可用),然后我会实际使用面向对象的方法。但是,我想将此保留为哈希的简单哈希

use warnings;
use strict;
use feature qw(say);

use Data::Dumper;

my %employee_hash;
<DATA>;  #Field Names
while ( my $employee_data = <DATA> ) {
    chomp $employee_data;
    my ($employee, $birth_date, $first_name, $last_name, $gender, $hire_date) = split /,/, $employee_data;
    $employee_hash{$employee}->{birth_date} = $birth_date;
    $employee_hash{$employee}->{first_name} = $first_name;
    $employee_hash{$employee}->{last_name}  = $last_name;
    $employee_hash{$employee}->{gender}     = $gender;
    $employee_hash{$employee}->{hire_date}  = $hire_date;
}

for my $employee ( sort keys %employee_hash ) {
    my $gender;
    if ( $employee_hash{$employee}->{gender} eq "M" ) {
        $gender = "he";
    }
    else {
        $gender = "she";
    }

    printf qq(Employee: %s is %s %s and %s was hired on %s\n),
        $employee,
        $employee_hash{$employee}->{first_name},
        $employee_hash{$employee}->{last_name},
        $gender,
        $employee_hash{$employee}->{hire_date};
}

__DATA__
emp_no,birth_date,first_name,last_name,gender,hire_date
10001,1953-09-02,Georgi,Facello,M,1986-06-26
10002,1964-06-02,Bezalel,Simmel,F,1985-11-21
10003,1959-12-03,Parto,Bamford,M,1986-08-28
10004,1954-05-01,Chirstian,Koblick,M,1986-12-01
10005,1955-01-21,Kyoichi,Maliniak,M,1989-09-12