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

This is similar to my last question but instead of input it is output. I am using:

my $fh = new IO::File "> output.csv"; foreach my $hash ( %hashed){ if($hashed{$hash}){ print "$hash has data $hashed{ $hash }->[0]\n"; $status = $csv->print($fh, $hashed{ $hash }); } }

This prints everything out to a csv file my problem is it prints everything out on 1 line instead of an individual line for each key of the hash. My has is a hash of array refs where the keys are unique id's from the array refs they point at. So, my question is am I just not using the print function correctly or is it something to do with my hash?

Replies are listed 'Best First'.
Re: printing CSV to file?
by trammell (Priest) on May 10, 2005 at 15:24 UTC
    Here's some code I've used to print to a CSV file:
    # $csv is an instance of Text::CSV_XS # $fh is an IO::File filehandle # %data is a row of data # @outcols corresponds to the keys of %data I want $csv->combine(@data{ @outcols }) || die "choked on: '@{[ $csv->error_input ]}'\n"; $fh->print($csv->string,"\n");
      Your code looks like a good solution to what I am doing however, I dono't think I have my data in a form that would work with that solution. my $csv is a Text::CSV_XS class I am reading in my data into hash that has keys that point to array refs. So I am reading in data like so:
      while(<$in>){ $csv->parse( $_ ) or warn "Bad data: $_\n", next; my @row = $csv->fields(); $hashed{ $row[ 0 ] } = \@row; }close $in;

      Update:Nevermind I was just being and idiot. Your solution worked great once I understood it thanks.

Re: printing CSV to file?
by dragonchild (Archbishop) on May 10, 2005 at 15:17 UTC
    What class is $csv an object of again?

    • In general, if you think something isn't in Perl, try it out, because it usually is. :-)
    • "What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?"
Re: printing CSV to file?
by Joost (Canon) on May 10, 2005 at 15:16 UTC
      $csv->print requires a IO::File or IO::Wrap to open the file to output. The problem I am running into is that there are no \n's at the end of each of my lines of data in my %hashed{key}->(ref'd array) so they all print out on 1 line in a csv file with the last column from the previous key and the first column from the current key squished together. So, now that I think about it is there a way to put a \n at the end of each of my lines of data in an efficient way as this could be handling over 40k lines of data?
Re: printing CSV to file?
by tphyahoo (Vicar) on May 10, 2005 at 16:15 UTC
    For csv, or any kind of delimited data, Text::Xsv is the way I go. Works real good.
Re: printing CSV to file?
by shemp (Deacon) on May 10, 2005 at 17:15 UTC
    I think that one problem with your code is that you really want:
    foreach my $hash (keys %hashed) { ...
    From your brief description im assuming that you want $hash to iterate over the keys in the hash, not both the keys and the values.
Re: printing CSV to file?
by jZed (Prior) on May 10, 2005 at 17:23 UTC
    Print the newline to the file handle after printing the data row:
    $csv->print($fh, $hashed{ $hash }); $fh->print("\n");