Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2024-04-18 06:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found