Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

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

More like the following without the need of a required first field. I took out the push since it was making every field an array ref. Also, this does both a hash and hash of hashes. Now to get arrays of hashes, and I'd have my all in one. 8)

It took me this long to figure all of this out, but I had a sick cat to deal with too. My sick cat takes precedence over my perl code.

sub make_list { my %opt = @_; my $file = $opt{file} && ref($opt{file}) eq 'ARRAY' ? data_file(@{$o +pt{file}}) : $opt{file}; my $raw_headers = $opt{'headings'} ? $opt{'headings'} : ['heading']; unshift @$raw_headers, 'key' if scalar @$raw_headers == 1; my %exapansions; my $headers = [map { s/\+$// && $exapansions{$_}++; $_ } @$raw_heade +rs]; my %list; my $csv = csv ( in => $file, headers => $headers, sep_char => '|', quote_char => undef, empty_is_undef => 1, allow_whitespace => 1, auto_diag => 1, on_in => sub { for my $header (@$headers) { if (scalar @$headers == 2) { $header eq $headers->[0] and next; $list{$_{$headers->[0]}} = $exapansions{$header} ? [split(m/ +;\s*/, $_{$header})] : $_{$header}; } else { $list{$_{$headers->[0]}}{$header} = $exapansions{$header} ? +[split(m/;\s*/, $_{$header})] : $_{$header}; } } } ); return \%list; }

Gives me ...

print DDumper make_list( file => $Artifacts, headings => ['+'] ); { 'Ark of the Covenant' => [ 'Raides of the Lost Ark', 'The Librarian: Quest for the Spear' ], '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 Crystal 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' ], Excalibur => [ 'Excalibur', 'The Last Legion', 'The Librarian: Quest for the Spear' ], 'H.G. Wells\' Time Machine' => [ 'The Librarians', 'Warehouse 13' ], 'Holy Grail' => [ 'Indiana Jones and the Last Crusade', 'The Librarian: Quest for the Spear' ], Necronomicon => [ 'H.P. Lovecraft' ], 'Pandora\'s Box' => [ 'Warehouse 13', 'The Librarian: Quest for the Spear' ], 'Spear of Destiny' => [ 'The Librarian: Quest for the Spear', 'Hellboy', 'Constantine' ], TARDIS => [ 'Doctor Who', 'The Sarah Jane Adventures', 'The Librarians, And the Final Curtain' ] }

And this (file)...

print DDumper make_list( file => $programs ); { Adobe => 'www.adobe.com/downloads', CCleaner => 'www.piriform.com/ccleaner', 'Cerulean Studios TrillianAstra' => 'www.trillian.im', Chrome => 'www.google.com/chrome', Debian => 'www.debian.org', Defraggler => 'www.piriform.com/defraggler', 'Ever Changing Book of Names' => 'ebon.pyorre.net', FileZilla => 'filezilla-project.org', Firefox => 'www.mozilla.com/en-US/firefox', 'Flash Player' => 'get.adobe.com/flashplayer', Geany => 'www.geany.org', Google => 'www.google.com', HexChat => 'hexchat.github.io', KDE => 'www.kde.org', LibreOffice => 'www.libreoffice.org', Mozilla => 'www.mozilla.org', 'Notepad++' => 'notepad-plus.sourceforge.net/uk/site.htm', Opera => 'www.opera.com', Piriform => 'www.piriform.com', QuickTime => 'www.apple.com/quicktime/download', RarLab => 'www.rarlab.com', Reader => 'get.adobe.com/reader', Safari => 'www.apple.com/safari/download', 'Strawberry Perl' => 'strawberryperl.com', TableSmith => 'mythosa.net/wiki/pmwiki.php?n=Main.TableSmith +', 'The Apache Software Foundation' => 'www.apache.org', 'VLC media player' => 'www.videolan.org/vlc/index.html', VideoLAN => 'www.videolan.org', grepWin => 'tools.tortoisesvn.net/grepWin.html', }

And even this (file)...

print DDumper make_list_2( file => $BBSs, headings => [qw(name domain +ip site)] ); { Abyss => { domain => 'abyss.monad.net', ip => '204.97.16.51', name => 'Abyss', site => undef }, 'Aloha BBS' => { domain => 'alohabbs.org.mx', ip => undef, name => 'Aloha BBS', site => undef }, Apocalypse => { domain => undef, ip => '208.215.35.204', name => 'Apocalypse', site => undef }, Astro => { domain => 'astro.sci.muni.cz', ip => undef, name => 'Astro', site => 'astro.sci.muni.cz' }, Atlantis => { domain => 'bbs.resus.univ-mrs.fr', ip => '139.124.29.1', name => 'Atlantis', site => undef }, }
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 Re^4: Can Text::CSV_XS return key-value pairs? by Lady_Aleena
in thread 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 browsing the Monastery: (4)
    As of 2019-05-26 17:59 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Do you enjoy 3D movies?



      Results (153 votes). Check out past polls.

      Notices?
      • (Sep 10, 2018 at 22:53 UTC) Welcome new users!