Writing multiple CSV files using Perl

I am trying to create multiple CSV files, which I'm going to use to input tables into Mysql (I had trouble writing code to put straight into mysql, and thought this would be easier for me to write, even if it is a bit convoluted) . The code compiles correctly and creates the files, but only the first file receives any data (and the first file goes into mysql fine).

use Text::CSV;
use IO::File;

my $GeneNumber = 4;
my @genearray;
my @Cisarray;
my @Cisgene;

$csv = Text::CSV->new ({ binary => 1, eol => $/ });
$iogene = new IO::File "> Gene.csv";
$iocis = new IO::File "> Cis.csv";
$iocisgene = new IO::File ">Cisgene.csv";

for(my $i=1; $i<=$GeneNumber; $i++)

        push(@genearray, 'Gene'.$i);
        push(@genearray, rand());
    push(@genearray, rand());

    my $CisNumber=int(rand(2)+1);

    for (my $j=1;$j<=$CisNumber;$j++){

      push(@Cisgene, 'Gene'.$i);
      push(@Cisgene, 'Cis'.$i.$j);

      my $cisgeneref = \@cisgeneref;

      $status = $csv->print ($iocisgene, $cisgeneref);

      push (@Cisarray, 'Cis'.$i.$j);
      push (@Cisarray, rand());

      my $cisref = \@cisref;

      $status = $csv->print ($iocis, $cisref);

    my $generef= \@genearray;

    $status = $csv->print ($iogene, $generef);

I am guessing the problem is something to do with

 $status = $csv->print ($iocisgene, $cisgeneref);

I tried creating three versions of:

 $csv = Text::CSV->new ({ binary => 1, eol => $/ });

however I still encountered the same problem.



This looks like one of those (very common) cases where adding use strict and use warnings to the top of your program will help you to track down the problem easily.

In particular, the lines:

my $cisgeneref = \@cisgeneref;


my $cisref = \@cisref;

look rather suspect as they take references to arrays (@cisgeneref and @cisref) which you have not used previously in the program.

I suspect that you really wanted:

my $cisgeneref = \@Cisgene;


my $cisref = \@Cisarray;

Attempting to write Perl code without use strict and use warnings is a terrible idea.

Why don't you simply print the string to the files, without those libs:

open GENE, ">", "Gene.csv" or print "Can't create new file: $!\n\n";
print GENE "This;could;be;a;CSV File\nWith;2 lines;and;5 columns;=)";
close GENE or print "Can't close file: $!";

P.S. In my definitions, my CSV don't use commas, but instead use semicolons. Also, @davorg sugestion of using strict and warnings is highly recommended.

Need Your Help

Grid not binding json data - MVC Kendo UI Grid bound via Ajax to a WebAPI and OpenAccess ORM

asp.net-web-api kendo-ui kendo-grid kendo-asp.net-mvc

Below is a sample of MVC Kendo UI Grid bound via Ajax to a WebAPI. The WebAPI uses OpenAccess ORM as data model.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.