Mercurial > repos > fgiacomoni > hmdb_ms_search
comparison lib/csv.pm @ 14:625fa968d99a draft
Master branch Updating - - Fxx
author | fgiacomoni |
---|---|
date | Thu, 10 Jan 2019 09:10:14 -0500 |
parents | 6d0a0f8f672a |
children |
comparison
equal
deleted
inserted
replaced
13:3587cb923890 | 14:625fa968d99a |
---|---|
192 my $self = shift ; | 192 my $self = shift ; |
193 my ( $csv, $file_name, $rows ) = @_ ; | 193 my ( $csv, $file_name, $rows ) = @_ ; |
194 | 194 |
195 my $fh = undef ; | 195 my $fh = undef ; |
196 $csv->eol ("\n"); ## end-of-line string to add to rows | 196 $csv->eol ("\n"); ## end-of-line string to add to rows |
197 # $csv->quote_char(undef) ; | |
198 open $fh, ">:encoding(utf8)", "$file_name" or die "$file_name: $!"; | |
199 | |
200 my $status = $csv->print ($fh, $_) for @{$rows}; | |
201 close $fh or die "$file_name: $!"; | |
202 | |
203 return(\$file_name) ; | |
204 } | |
205 ## END of SUB | |
206 | |
207 | |
208 =head2 METHOD write_csv_from_hash | |
209 | |
210 ## Description : write a csv file from simple hash with headers | |
211 ## Input : $csv, $file_name, $headers, $arrayofhash | |
212 ## Output : $csv_file | |
213 ## Usage : my ( $csv_file ) = write_csv_from_arrays( $csv, $file_name, $headers, $arrayofhash ) ; | |
214 | |
215 =cut | |
216 ## START of SUB | |
217 sub write_csv_from_array_of_hash { | |
218 ## Retrieve Values | |
219 my $self = shift ; | |
220 my ( $csv, $file_name, $headers, $hash ) = @_ ; | |
221 | |
222 my $fh = undef ; | |
223 $csv->eol ("\n"); ## end-of-line string to add to rows | |
197 $csv->quote_char(undef) ; | 224 $csv->quote_char(undef) ; |
198 open $fh, ">:encoding(utf8)", "$file_name" or die "$file_name: $!"; | 225 open $fh, ">:encoding(utf8)", "$file_name" or die "$file_name: $!"; |
199 | 226 |
200 my $status = $csv->print ($fh, $_) for @{$rows}; | 227 if (defined $headers) { |
228 for my $field (@$headers) { print $fh join('\t', $field), "\n"; } ; | |
229 } | |
230 | |
231 for my $row (keys %{$hash}) { | |
232 print $fh join('\t', map { $_, $row->{$_} } sort keys %$row), "\n"; | |
233 } | |
234 | |
201 close $fh or die "$file_name: $!"; | 235 close $fh or die "$file_name: $!"; |
202 | 236 |
203 return(\$file_name) ; | 237 return(\$file_name) ; |
204 } | 238 } |
205 ## END of SUB | 239 ## END of SUB |