我正在尝试将记录插入mysql数据库。我有多个段落的文本文件,一个段落必须是一个db记录。
我的数据库有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
答案 0 :(得分: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];