2013-06-25 37 views
0

我在使用Perl将以下csv代码片段转换为哈希值时遇到了问题。将csv转换为阵列的perl哈希值

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; 

} 
+1

什么是你的问题?如果您无法使脚本正常工作,请显示您尝试过的内容。 – Barmar

+5

将它转换为散列数组而不是散列数组不是更好吗? – Barmar

+0

请参阅http://search.cpan.org/~makamaka/Text-CSV-1.32/lib/Text/CSV.pm了解处理CSV文件的包。 – Barmar

回答

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

只能使用这个,如果你因为某些原因不能使用 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); 
} 
+0

+1指出**有一个模块来做到这一点** – lexu

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