Lady_Aleena has asked for the wisdom of the Perl Monks concerning the following question:

Hello. I'm currently rewriting one of my more loathed subs with Text::CSV_XS. I was wondering if there were a way to write it so that it returns key-value pairs instead of an array or hash of hashes when there are only two fields in the file.

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' ] };

Is it possible with Text::CSV_XS?

No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
Lady Aleena