0

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++)
    {
        @genearray=();

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

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

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

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

      my $cisgeneref = \@cisgeneref;

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

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

      my $cisref = \@cisref;

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

    my $generef= \@genearray;

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

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.

Thanks

2 Answers 2

2

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;

and:

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;

and:

my $cisref = \@Cisarray;

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

This is fine as long as you can be certain that one day someone will slip in a ; into your field data without you noticing, or with you noticing only after a full day of trying to find the problem somewhere else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.