http://www.perlmonks.org?node_id=482414


in reply to Re: Process and combine two CSV files into one
in thread Process and combine two CSV files into one

I've got the two files combined in an array. If I use Dumper I can see the contents of the array and they are correct. My problem now is how to print them out in a CSV format. This is what I tried:

my @row = $dbh->selectall_arrayref("SELECT IP, Domain, ServerName, Day +sUptime, OS, RAM, OSSP, InstallDate, CPUSpeed, CPUCount, CPUType FROM + hosts INNER JOIN info ON hosts.IP = info.IP"); #print Dumper @row; foreach my $data ( @row ) { print("$data"); };
How can I get each row of the array to print out on a seperate line each?

Replies are listed 'Best First'.
Re^3: Process and combine two CSV files into one
by Thargor (Scribe) on Aug 09, 2005 at 21:23 UTC

    hmmm if you are just wondering on how to print to a csv file you can just use the IO::File and Text::CSV_XS. If you go check out the documentation of Text::CSV_XS and IO::File what happens is that you scan in your csv file 1 line at a time and each line is put into an array. Anyways, you can do something like

    use Text::CSV_XS; use IO::File; use strict; my $csv = Text::CSV_XS->new({sep_char => "\t"}); my %hashed; open my $in, '<', $input1 or die "Read Failed: $!\n"; while(<$in>) { $csv->parse( $_ ) or warn "Bad data: $_\n", next; my @row = $csv->fields(); $hashed{ $row[0] } = \@row; } close $in; my $fh = new IO::File "> $output"; foreach my $hash ( %hashed) { if($hashed{$hash}) { $csv->combine(@{$hashed{$hash}}); print "$hash has data $hashed{ $hash }->[0]\n"; $fh->print($csv->string, "\n"); } } $fh->close;

    This is just a basic read in of a csv file that is tab separated. All of the input and csv manipulation can be found in Text::CSV_XS documentation. But to output the file in csv format the easiest way I found was with IO::File.

      Great suggestion, but I don't think it's what I'm looking for. Then again, maybe I don't understand what you've given me. I'm using SQL to combine my CSV files based upon whats in them. The array that's created is what I need to print out. how can I use your example to print out the table in my array?

        Ah sorry didn't notice that you were using only SQL data stuff. My suggestion was only if you need to do some compare and combining of csv files outside of a database.