Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

comment on

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

Hello. In my effort to improve my perl writing, I was going to take some of my own advice and stop trying to find reasons to not use something. With that in mind, I finally decided to look into Text_CSV_XS to replace my hand rolled flat file processor. I gravitated right to the function csv in the module as it looked easier to use. However, I came upon a problem. In the POD it says that when key is set, headers is set to auto, so the headers I set would not work.

So, other than going through every flat file I have and adding the headers, can anyone think of a way to work around? I have so very many flat files and could possibly miss a few, so adding headers in now would be problematic.

What I want is a hash of hashes with the first item on each line to be the key to the hash along with being a named value in the hash.

Is there maybe someone who would hold my hand as I try to work through the method in Text::CSV_XS?

My data

Crystal Skull|Stargate SG-1, Crystal Skull;The Librarian: Return to Ki +ng Soloman's Mines;Indiana Jones and the Kingdom of the Crystal Skull Spear of Destiny|The Librarian: Quest for the Spear;Hellboy;Constantin +e Book or Key of Soloman|The Librarian: Return to King Soloman's Mines;S +eason of the Witch Necronomicon|H.P. Lovecraft

My code

#!/usr/bin/perl use strict; use warnings; use Text::CSV_XS qw(csv); my $file = '/home/me/Documents/www/files/data/Miscellany/Magic_items.t +xt'; open(my $fh, '<', $file) || die "Can't open $file $!"; my $list = csv ( in => $file, sep_char => '|', headers => ['item', 'seen in'], key => ['item'], ); use Data::Dumper; print Dumper $list;

My result

Use of uninitialized value in anonymous hash ({}) at /home/me/perl5/li +b/perl5/x86_64-linux-gnu-thread-multi/Text/ line 1223, <$fh> + line 4. Use of uninitialized value in anonymous hash ({}) at /home/me/perl5/li +b/perl5/x86_64-linux-gnu-thread-multi/Text/ line 1223, <$fh> + line 4. Use of uninitialized value in anonymous hash ({}) at /home/me/perl5/li +b/perl5/x86_64-linux-gnu-thread-multi/Text/ line 1223, <$fh> + line 4. Use of uninitialized value in anonymous hash ({}) at /home/me/perl5/li +b/perl5/x86_64-linux-gnu-thread-multi/Text/ line 1223, <$fh> + line 4. $VAR1 = { '' => { 'seen in' => 'H.P. Lovecraft', 'item' => 'Necronomicon' } };

Update: This is as far as I have gotten with the method version.

$csv = Text::CSV_XS->new ({ sep_char => '|', quote_char => undef, blank_is_undef => 1, empty_is_undef => 1, allow_whitespace => 1, });
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 Text::CSV_XS function does not accept headers and key together by Lady_Aleena

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?

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

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (5)
    As of 2019-09-15 12:48 GMT
    Find Nodes?
      Voting Booth?
      The room is dark, and your next move is ...

      Results (180 votes). Check out past polls.