在数组的perl散列中转换csv

在数组的perl散列中转换csv,perl,perl-module,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,Koblic

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

}
while(){
@第一行的keys=split/,/,$如果$.==1;#
@行=拆分/,/,$;
按@hash{@keys},@row;
}
类似于:

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.
}
比如:

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.
}

只有当你因某种原因不能使用时才使用此选项 模块:)

my%员工;
while()){
下一个if/^emp/;
我的@r=split/,/;
推送{$employee{${}},移位@r
对于qw(emp无出生日期首名姓氏性别雇佣日期);
}

仅当您因某种原因无法使用时才使用此选项 模块:)

my%员工;
while()){
下一个if/^emp/;
我的@r=split/,/;
推送{$employee{${}},移位@r
对于qw(emp无出生日期首名姓氏性别雇佣日期);
}

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
,然后我将实际使用面向对象的方法。但是,我想将其保留为一个简单的散列:

使用警告;
严格使用;
使用特征qw(例如);
使用数据::转储程序;
我的%employee\u哈希;
;  #字段名
while(我的$employee_data=){
chomp$employee_数据;
我的($employee,$birth\u date,$first\u name,$last\u name,$gender,$hire\u date)=拆分/,/,$employee\u数据;
$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;
}
对于我的$employee(排序键%employee\u散列){
我的性别;
if($employee_hash{$employee}->{gender}eq“M”){
$gender=“he”;
}
否则{
$gender=“她”;
}
printf qq(员工:%s是%s%s,并且%s是在%s上雇用的\n),
$employee,
$employee_hash{$employee}->{first_name},
$employee_hash{$employee}->{last_name},
$性别,
$employee_hash{$employee}->{hire_date};
}
__资料__
emp编号、出生日期、名、姓、性别、雇用日期
100011953-09-02,密歇根州费塞洛市格奥尔基,1986-06-26
100021964-06-02,贝扎勒,辛美尔,F,1985-11-21
100031959-12-03,马里兰州班福德市帕托,1986-08-28
100041954-05-01,密歇根州科布利克市奇尔斯蒂安,1986-12-01
100051955-01-21,缅因州马里尼亚克京池,1989-09-12

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
,然后我将实际使用面向对象的方法。但是,我想将其保留为一个简单的散列:

使用警告;
严格使用;
使用特征qw(例如);
使用数据::转储程序;
我的%employee\u哈希;
;  #字段名
while(我的$employee_data=){
chomp$employee_数据;
我的($employee,$birth\u date,$first\u name,$last\u name,$gender,$hire\u date)=拆分/,/,$employee\u数据;
$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;
}
对于我的$employee(排序键%employee\u散列){
我的性别;
if($employee_hash{$employee}->{gender}eq“M”){
$gender=“he”;
}
否则{
$gender=“她”;
}
printf qq(员工:%s是%s%s,并且%s是在%s上雇用的\n),
$employee,
$employee_hash{$employee}->{first_name},
$employee_hash{$employee}->{last_name},
$性别,
$employee_hash{$employee}->{hire_date};
}
__资料__
emp编号、出生日期、名、姓、性别、雇用日期
100011953-09-02,密歇根州费塞洛市格奥尔基,1986-06-26
100021964-06-02,贝扎勒,辛美尔,F,1985-11-21
100031959-12-03,马里兰州班福德市帕托,1986-08-28
100041954-05-01,密歇根州科布利克市奇尔斯蒂安,1986-12-01
100051955-01-21,缅因州马里尼亚克京池,1989-09-12

你的问题是什么?如果您的脚本无法正常运行,请展示您尝试过的内容。这不是更好吗