Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Comment on

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

Not knowing Text::CSV_XS other than browsing the perldoc real quick, I can mainly only comment on your code. The main thing I see is mixing lists and ARRAYrefs, and lots of expensive copying back and forth between the two. Here's the first problem:

# Create ARRAYref to list of 7 elements, with first element "", # remaining 6 elements copied from @fields. $file2{$id}=["",@fields]; $file3{$id}=["",@fields]; ... # trying to access 6th element of $file2{$id} as a list, but it's # an ARRAYref to a list with 7 elements... my @fields2=$file2{$id}[5]; my @fields3=$file3{$id}[5];

You are constructing $file2{$id} as an ARRAYref with [] (correctly). But you're not dereferencing it on access. Why add the "" at the beginning of the list? You're probably looking for something more like:

# just hang onto the ARRAYref returned from $csv->getline(), # unless it doesn't want us to. $file2{$id}=$row; $file3{$id}=$row; ... # You really did want the 6th column, right? my $fields2=$file2{$id}->[5]; my $fields3=$file3{$id}->[5];

Next, this is likely not what you expect:

# Create list with one element as a string concat looking like # "ARRAY(0x141ecf8)\tARRAY(0x141edd0)\tARRAY(0x141ee48)" my @printline= ( $fields_ref."\t".\@fields2."\t".\@fields3; $csv->print ($FILE4,\@printline);

You've got a string consisting of three ARRAYrefs joined by tabs. So that does match the errors you're seeing. It's probably looking for something simpler, a single reference to a list with all the data in it. Probably looking for something more like:

# List with 8 elements my @printline= ( @fields, $file2{$id}->[5], $file3{$id}->[5] ); $csv->print ($FILE4,\@printline);

Last, you need more error checking imo.

Ultimately, I would recommend taking it a step further and just use references throughout, rather than copying lists to refs and back. It's just not necessary, and it causes your syntax to have to switch back and forth as well. References will keep your syntax consistent. There are lots of other ways to optimize this pretty significantly, but I'll leave that to you.

--Dave


In reply to Re: Expected fields to be an array ref by armstd
in thread Expected fields to be an array ref by bluray

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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 romping around the Monastery: (7)
    As of 2014-08-01 06:42 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (257 votes), past polls