Mercurial > repos > fgiacomoni > hmdb_ms_search
diff lib/csv.pm @ 14:625fa968d99a draft
Master branch Updating - - Fxx
author | fgiacomoni |
---|---|
date | Thu, 10 Jan 2019 09:10:14 -0500 |
parents | 6d0a0f8f672a |
children |
line wrap: on
line diff
--- a/lib/csv.pm Tue Oct 02 04:59:13 2018 -0400 +++ b/lib/csv.pm Thu Jan 10 09:10:14 2019 -0500 @@ -194,10 +194,44 @@ my $fh = undef ; $csv->eol ("\n"); ## end-of-line string to add to rows +# $csv->quote_char(undef) ; + open $fh, ">:encoding(utf8)", "$file_name" or die "$file_name: $!"; + + my $status = $csv->print ($fh, $_) for @{$rows}; + close $fh or die "$file_name: $!"; + + return(\$file_name) ; +} +## END of SUB + + +=head2 METHOD write_csv_from_hash + + ## Description : write a csv file from simple hash with headers + ## Input : $csv, $file_name, $headers, $arrayofhash + ## Output : $csv_file + ## Usage : my ( $csv_file ) = write_csv_from_arrays( $csv, $file_name, $headers, $arrayofhash ) ; + +=cut +## START of SUB +sub write_csv_from_array_of_hash { + ## Retrieve Values + my $self = shift ; + my ( $csv, $file_name, $headers, $hash ) = @_ ; + + my $fh = undef ; + $csv->eol ("\n"); ## end-of-line string to add to rows $csv->quote_char(undef) ; open $fh, ">:encoding(utf8)", "$file_name" or die "$file_name: $!"; - my $status = $csv->print ($fh, $_) for @{$rows}; + if (defined $headers) { + for my $field (@$headers) { print $fh join('\t', $field), "\n"; } ; + } + + for my $row (keys %{$hash}) { + print $fh join('\t', map { $_, $row->{$_} } sort keys %$row), "\n"; + } + close $fh or die "$file_name: $!"; return(\$file_name) ;