Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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

In reply to Can Text::CSV_XS return key-value pairs? by Lady_Aleena

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (9)
    As of 2019-06-24 20:28 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Is there a future for codeless software?



      Results (99 votes). Check out past polls.

      Notices?