My code so far...
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use Text::CSV_XS qw(csv);
sub make_list {
my (%opt) = @_;
my $headers = $opt{'headings'};
my $filters;
for my $header (@$headers) {
if ($header =~ s/\+$//) {
$filters->{$header} = sub { $_ = [split(/;\s*/, $_)]; $_ };
}
}
my $list = csv (
in => $opt{file},
headers => $headers,
key => $opt{key} ? $headers->[0] : undef,
filter => $filters,
sep_char => '|',
quote_char => undef,
empty_is_undef => 1,
allow_whitespace => 1,
) || die Text::CSV_XS->error_diag;
return $list;
}
my $file = 'Artifacts.txt';
my $headers = ['item', 'seen in+'];
print Dumper make_list( file => $file, headings => $headers);
The file...
Crystal Skull|Stargate SG-1, Crystal Skull; The Librarian: Return to K
+ing Soloman's Mines; Indiana Jones and the Kingdom of the Crystal Sku
+ll
Spear of Destiny|The Librarian: Quest for the Spear; Hellboy; Constant
+ine
Book or Key of Soloman|The Librarian: Return to King Soloman's Mines;
+Season of the Witch
Necronomicon|H.P. Lovecraft
Ark of the Covenant|Raides of the Lost Ark; The Librarian: Quest for t
+he Spear
TARDIS|Doctor Who; The Sarah Jane Adventures; The Librarians, And the
+Final Curtain
Doc Brown's Delorean|Back to the Future; Back to the Future Part II; B
+ack to the Future Part III; The Librarians, And the Final Curtain
Pandora's Box|Warehouse 13; The Librarian: Quest for the Spear
Excalibur|Excalibur; The Last Legion; The Librarian: Quest for the Spe
+ar
H.G. Wells' Time Machine|The Librarians; Warehouse 13
Holy Grail|Indiana Jones and the Last Crusade; The Librarian: Quest fo
+r the Spear
So what I want from this file is the first field to be the keys and the second field to be an array ref. Here is an example of what I want.
$VAR1 = {
'Ark of the Covenant' => [
'Raides of the Lost Ark',
'The Librarian: Quest for the Spe
+ar'
],
'Necronomicon' => [
'H.P. Lovecraft'
],
'Holy Grail' => [
'Indiana Jones and the Last Crusade',
'The Librarian: Quest for the Spear'
],
'H.G. Wells\' Time Machine' => [
'The Librarians',
'Warehouse 13'
],
'Spear of Destiny' => [
'The Librarian: Quest for the Spear'
+,
'Hellboy',
'Constantine'
],
'Book or Key of Soloman' => [
'The Librarian: Return to King
+ Soloman\'s Mines',
'Season of the Witch'
],
'Crystal Skull' => [
'Stargate SG-1, Crystal Skull',
'The Librarian: Return to King Soloman\
+'s Mines',
'Indiana Jones and the Kingdom of the C
+rystal Skull'
],
'Doc Brown\'s Delorean' => [
'Back to the Future',
'Back to the Future Part II',
'Back to the Future Part III',
'The Librarians, And the Final
+Curtain'
],
'TARDIS' => [
'Doctor Who',
'The Sarah Jane Adventures',
'The Librarians, And the Final Curtain'
],
'Pandora\'s Box' => [
'Warehouse 13',
'The Librarian: Quest for the Spear'
],
'Excalibur' => [
'Excalibur',
'The Last Legion',
'The Librarian: Quest for the Spear'
]
};