I have no idea what you did but it gives me a place to start and learn.

Yes, XML::Rules does take a bit of studying its docs to understand all the rules. As for my XML::LibXML code, I hope that's a bit more clear. If you add use Data::Dump; at the top and add a dd \%items; just after the loop, you'll see the data structure:

{ "00123" => { "File 1" => "www.site.com/path/to/file/file.pdf", "File 2" => "www.site.com/path/to/file/file.pdf", }, "00124" => { "File 1" => "www.site.com/path/to/file/file.pdf", "File 6" => "www.site.com/path/to/file/file.pdf", }, "00125" => { "File 2" => "www.site.com/path/to/file/file.pdf", "File 7" => "www.site.com/path/to/file/file.pdf", }, }

The other tricky bit is the line that generates the @columns array. It has two main parts: map {keys %$_} values %items takes the values of the %items, which are hashes themselves ({ "File 1" => "www..." etc.), and then uses map to get the keys of each of those hashes. In other words, the map statement will return the list "File 1", "File 2", "File 1", "File 6", .... The second part is the keys %{{map {$_=>1} ...}}, which is an idiom to get only unique values from a list. It does this by first building an anonymous hash with {map {$_=>1} values}, and then immediately dereferencing the hash (%{...}) and getting its keys. Then, all that's left to do is sort the unique strings.

I’m trying to print this out to a file but I’m getting errors about print usage.

What hippo said: first, you're opening the file for reading instead of writing. Second, note that the first argument of $csv->print should be the filehandle, which you did, but note that I was getting the output filehandle (STDOUT by default) using select, so you need to remove that call. This works for me:

open my $fh, ">:encoding(UTF-8)", "output.csv" or die "output.csv: $!" +; my $csv = Text::CSV->new({binary=>1, auto_diag=>2, eol=>$/, always_quote=>1 }); $csv->print($fh, ["sku", @columns]); for my $sku (sort keys %$itms) { $csv->print($fh, [$sku, map { $itms->{$sku}{$_} } @columns ]); } close $fh;

In reply to Re^3: Logic Help - Output to csv by haukex
in thread Logic Help - Output to csv by audioboxer

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, details, 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, summary, 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.