tkn has asked for the wisdom of the Perl Monks concerning the following question:

the new csv file that i generate out is empty. pls help to check.
sub edit() { $file = $File::Find::name; if ($file=~/$thecode\.csv/i) { print "CSV File : $File::Find::name\n"; print "$file"; $CSV_file = $File::Find::name; } } if ($rim1_thecode ne "") { ##generate RIM1 csv file## open (CSVIN, "<$CSV_file") or die "Can't open $CSV_file: $!\n"; $outcsv_file = $rim1_thecode."\.csv"; open (CSVOUT, ">$outcsv_file") or die "Can't open $outcsv_file: $!\ +n"; @lines=<CSVIN>; for $line(@lines) { ##substitute $thecode with RIM1 transaction code## $line =~ s/$thecode/$rim1_thecode/; print CSVOUT $line; } close CSVIN; close CSVOUT; }

Edited by Chady -- added code tags.

Replies are listed 'Best First'.
Re: generate new files
by McDarren (Abbot) on Jan 07, 2006 at 04:32 UTC
    Hi tkn, and welcome to Perl Monks :)

    Firstly, it would be good if you could add <code> tags around your code. This will make it much easier to read. See Writeup Formatting Tips for more info on that.

    Secondly, you could help us to help you if you provided a little more information. For example, what do $thecode and $rim1_thecode variables contain? (if anything). Is your code running with use strict and warnings enabled?

    The most likely cause of your problem is that the $line variable is empty or undefined when you go to print it to the filehandle, but it's impossible to say for certain with the (lack of) information you've given.

    Have a read of I know what I mean. Why don't you? and How (Not) To Ask A Question for some more tips on asking your question in such a way as to encourage good answers.

    Cheers,
    Darren :)

Re: generate new files
by ambrus (Abbot) on Jan 07, 2006 at 13:49 UTC

    I'm sorry, but can't tell what the error is from this code. (Try reading the other replies, maybe I failed to notice an obvious errors.)

    My guess is that you might not be opening the right input CSV file; or that you are opening more than one input files but write the result to the same output file and the last input file is empty or unreadable or is a directory; or that writing to the output file fails because the disk or quota is full (this has happened to me). Note that as you are opening the output with > mode, you overwrite it with every open.

    You might try adding debugging statements to see what the code does, for example something like this might help. If you see that the same output file is reported multiple times or that the input file is empty, you'll be able to know where to search further.

    if ($rim1_thecode ne "") { ##generate RIM1 csv file## open (CSVIN, "<$CSV_file") or die "Can't open $CSV_file: $!\n"; $outcsv_file = $rim1_thecode."\.csv"; open (CSVOUT, ">$outcsv_file") or die "Can't open $outcsv_file: $!\ +n"; warn "transforming $CSV_file -> $outcsv_file"; @lines=<CSVIN>; warn "read ", 0 + @lines, " lines"; for $line(@lines) { ##substitute $thecode with RIM1 transaction code## $line =~ s/$thecode/$rim1_thecode/; print CSVOUT $line; } close CSVIN; close CSVOUT; }
Re: generate new files
by McDarren (Abbot) on Jan 08, 2006 at 16:06 UTC
    Okay, I have had a closer look at this one.

    First of all, that edit sub seems to be a bit of a red herring, as it appears to have nothing to do with the rest of the code.

    I have modified the rest of the code so it will run standalone, and enabled strict and warnings.

    I also created a dummy cvsin file to read from.

    So, we have....

    darren@barney:~/perlmonks$ cat csvin replace_me merry xmas hello world perlmonks replace_me just another Perl hacker,

    And the modified code...

    #!/usr/bin/perl -w use strict; my $rim1_thecode = "csvout"; my $CSV_file = "csvin"; my $thecode = "replace_me"; if ($rim1_thecode ne "") { ##generate RIM1 csv file## open (CSVIN, "<$CSV_file") or die "Can't open $CSV_file: $!\n"; my $outcsv_file = $rim1_thecode."\.csv"; open (CSVOUT, ">$outcsv_file") or die "Can't open $outcsv_file: $! +\n"; my @lines = <CSVIN>; for my $line (@lines) { ##substitute $thecode with RIM1 transaction code## $line =~ s/$thecode/$rim1_thecode/; print CSVOUT $line; } close CSVIN; close CSVOUT; }

    Which when run creates the file csvout.csv, which looks like so..

    darren@barney:~/perlmonks$ cat csvout.csv csvout merry xmas hello world perlmonks csvout just another Perl hacker,

    As you can see, all the instances of "replace_me" have been replaced with "csvout", which is exactly as expected. However, I'm quite certain this isn't what you are trying to achieve. I think what you need to do now is explain as clearly as possible precisely what it is you are trying to do, and then we may be able to help you further.

    Cheers,
    Darren :)