0

我在使用 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;

}
4

3 回答 3

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);   
}
于 2013-06-25T21:30:26.260 回答
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.
}
于 2013-06-25T19:04:56.277 回答
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 有一个很好的教程。我建议你阅读它。

实际上,您可能希望存储以员工编号为键的数据,因此您需要一个哈希值。

这是哈希哈希的示例。注意:这不是我做这个程序的方式。首先,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
于 2013-06-26T16:55:21.613 回答