0

我正在尝试将记录插入 mysql 数据库。我有多个段落的文本文件,一个段落必须是一个数据库记录。

我的数据库有 5 列 - 职业、经验、部门、被保险人、备注

我的文本文件具有以下格式 - 我有一个 perl 代码监视存储这些文本文件的目录。问题是可能有两个职业值或只有一个。到目前为止,当格式只有一个占用值时,我开始从第 n 个索引读取文件数组,在循环中提取偏移量为 5 的所有内容,使段落成为记录。

格式不会改变,它将是以下两段的组合。如何将这些段落分别放入数组中以进行数据库插入?谢谢!~拉尔夫

### Start of File
Header

Occupation: Analyst3.
Experience: 7
Department: ZAD6A.
Insured: 0
Remarks: None


Occupation: Analyst2.
Occupation: Engineer-I.
Experience: 4
Department: 50021.
Insured: 0
Remarks: New Hire.

Footer
### End Of File
4

1 回答 1

0
# split the input string into blocks by one or more empty lines
my @blocks = split /\n{2,}/, $input;

# skip Header and Footer by shifting index of @blocks
# get all the value after ':' in the same block
# merge the 1th and 2nd value of the array for duplicated occupation if size == 6
my @results = map { @{$_} == 6 ? ["$_->[0]:$_->[1]", @{$_}[2..5]] : $_ } 
    map { [/:\s*([^\n]+)/g] } @blocks[1..$#blocks-1];
于 2013-08-07T23:15:23.947 回答