my $ar_data = []; #Opening, reading, and extracting column content of each concord.n.txt file foreach my $output_concord_file (@output_concord_files){ #Note : for the concord file, no processing implied -> stored by default in $output_concord_file open (my $fh, '<:utf8', "$output_concord_file") || die "Couldn't open $output_concord_file : $!"; my $hr_output = {}; $hr_output->{concord_file} = $output_concord_file; while (<$fh>){ if ($_ =~ /=\[=(.*)=\]=/){ $hr_output->{url} = $1; } if ($_ =~ /=\[\+(.*)\+\]=/){ $hr_output->{sys_time} = $1; } if ($_ =~ /=E-(.*)=event/){ push(@{$hr_output->{events}}, $1); } } push(@{$ar_data}, $hr_output); close ($fh); } #### if (@{$ar_data}) { # If there is any data, prepare the query my @fields = keys %{$ar_data->[0]}; # Extract all the keys from the first element of ar_data my $fieldlist = join(',', @fields); my $field_placeholders = join(',', map {'?'} @fields); my $sth_insert_article = $dbh->prepare( qq( INSERT INTO article ($fieldlist) VALUES ($field_placeholders) )); my $sth_select_last_insert_id = $dbh->prepare( qq( SELECT LAST_INSERT_ID )); my $sth_insert_event = $dbh->prepare( qq( INSERT INTO event (event) VALUES (?) )); my $sth_insert_article_event = $dbh->prepare( qq( INSERT INTO article_event_index (id_article, id_event) VALUES (?,?) )); # Now we loop over and insert all data foreach my $hr_output (@{$ar_data}) { # Insert article data my $inserted_records = $sth_insert_article->execute(@{$hr_output}{@fields}); # Ok, I admit, this one is really tricky to read :) if ($inserted_records != 1) { die "Error inserting article [$hr_output->{url}], only [$inserted_records] got inserted: " . $sth_insert_article->errstr; } # Get article ID my $article_id = ($dbh->selectcol_arrayref($sth_select_last_insert_id))->[0]; # Insert events foreach my $event (@{$hr_output->{events}}) { $inserted_records = $sth_insert_event->execute($event); if ($inserted_records != 1) { die "Error inserting event [$event], only [$inserted_records] got inserted: " . $sth_insert_event->errstr; } # Get event ID my $event_id = ($dbh->selectcol_arrayref($sth_select_last_insert_id))->[0]; # Insert article_event combo $inserted_records = $sth_insert_article_event->execute($article_id, $event_id); if ($inserted_records != 1) { die "Error inserting article-event [$article_id, $event_id], only [$inserted_records] got inserted: " . $sth_insert_article_event->errstr; } } } }