* ccc31807 <
fcce762a-70a6-47db...@a1g2000yqd.googlegroups.com> :
Wrote on Tue, 29 May 2012 07:59:55 -0700 (PDT):
| open OUT1, '>', 'csvoutput.csv';
| open OUT2, '>', 'pdfoutput.pdf';
| foreach my $key (sort keys %data_hash_out)
| {
| print_to_csv($data_hash_out{$key});
| print_to_pdf($data_hash_out{$key});
| }
| close OUT1;
| close OUT2;
If you want to use multiple files without nesting calls to
with-open-file, you could use OPEN as in this sketch:
(let (out1 out2)
(unwind-protect
(progn
(setq out1 (OPEN "csvoutput.csv" :direction :output :if-exists
:supersede))
(setq out2 (OPEN "pdfoutput.pdf" :direction :output :if-exists
:supersede))
(loop for key being each hash-key of $data_hash_out
using (hash-value val)
do (print-csv val out1)
(print-pdf val out2)))
;; unwind forms
(unless (null out1) (close out1))
(unless (null out2) (close out2))))
Othertimes when you want to decouple OPEN from unwind-close pattern, you
can use the WITH-OPEN-STREAM macro, giving it the open stream; and the
macro which will automatically CLOSE the file, when the forms are
done. This just lets you avoid spelling out the UNWIND-PROTECT form
explicitly.
| My mental image of Perl is a sequential set of steps, called one after
| another, processing lines iteratively. My mental image of LISP is an
| outer function calling inner functions processing lines recursively.
| Either this is an incorrect mental image of LISP or I'm not yet
| equipped to write in proper LISP style (most likely).
|
file operations are the same in any language. The specific problem you
are trying to solve (beyond figuring out the file system syntax), may be
amenable to mapping solutions (i.e. map this function across all
records). However for the tasks you have exhibited here over the years,
what you could do with lisp (in employing this style), you can also do
in perl.
--- Madhu