Okay, adding test data, fixing most of the syntax errors, breaking out the steps, and adding debugging statements (collectively referred to as "Troubleshooting by firepower" by an old friend in the business), we have some clues for you to examine:

#!/usr/bin/perl -w use strict; # Set up test data my @header = ( 'Name', 'DOB', 'DOJ', 'Country', 'Region', 'Salary', 'Place', ); my @data = ( 'JKSLK', 19890101, 20000101, 'France', 'ALMERIA', 100000.00, 'BCFGHK', ); my $empid = 123456; # Set up working data my @report = (); my @array = (); # Initialize push @array, @header; # Do the work my $csvfile; # SKM: What is this for, exactly? my $reportLine = join "\t", ($empid, @array); print "DEBUG: \$reportLine = [$reportLine]\n"; push @report,$reportLine,"\n"; foreach my $reportElement (@report) { print "DEBUG: \$reportElement = [$reportElement]\n"; } foreach (@report) { print $_; print $csvfile $_; } exit; __END__

This produces:

C:\Steve\Dev\PerlMonks\P-2013-07-11@1436-CSV-Fail>perl csvfail2.pl DEBUG: $reportLine = [123456 Name DOB DOJ Country Region + Salary Place] DEBUG: $reportElement = [123456 Name DOB DOJ Countr +y Region Salary Place] DEBUG: $reportElement = [ ] Can't use an undefined value as a symbol reference at csvfail2.pl line + 54. 123456 Name DOB DOJ Country Region Salary Place

My observations:

  1. The tabs are in the data. You already knew that, of course, since when you display it directly to the console you get the tabs. Your real issue is in how to get the information into a file, as you have stated. But there are other issues here which should be resolved first.
  2. You don't seem to have the syntax figured out for writing to a file from Perl. Suggest you review the open, print, and close functions in the documentation, and look for code examples. It is rare to need to a scalar in place of a hard-coded file handle.
  3. You are putting the newline on its own element in @report instead of simply appending it to the line. This is probably not what you intended, though it's not technically an error.
  4. $empid is handled oddly here; I can see a number of reasons you are including it in the snippet, but without a functioning snippet, they are just guesses.
  5. It looks like you are building a prototype of the logic to be used inside a loop. Not a bad approach, but coding up a fake loop is very little effort and in my humble opinion is better than writing linear code and then converting it later into a loop. Your mileage may vary.

I don't want to just solve the problem for you, since the point of PerlMonks is, generally, to help you become a better Perl programmer. That is best done if you do some of the analysis yourself.

I'd be happy to answer any questions you have about what this example is showing us, or adjustments to the code which more clearly refine the scope of your question.


In reply to Re^2: Write array contents to csv file by marinersk
in thread Write array contents to csv file by learner@perl

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.