Database Reference
In-Depth Information
After invoking
execute()
, the column labels are available in
$sth->{NAME}
(see
Recipe 10.2
). To produce each line of output, use
combine()
and
string()
. The
com
bine()
method takes an array of values and converts them to a properly formatted
string.
string()
returns the string so we can print it.
11.6. Converting Datafiles from One Format to Another
Problem
You want to convert a file to a different format to make it easier to work with, or so that
another program can understand it.
Solution
Use the
cvt_file.pl
conversion script described here.
Discussion
The
mysql_to_text.pl
script discussed in
Recipe 11.5
uses MySQL as a data source and
produces output in the format you specify via the
--delim
,
--quote
, and
--eol
options.
This section describes
cvt_file.pl
, a utility that provides similar formatting options, but
for both input and output. It reads data from a file rather than from MySQL, and converts
it from one format to another. This enables the script to serve as a bridge between
operations that use different formats. For example, invoke
cvt_file.pl
as follows to read
a tab-delimited file
data.txt
, convert it to colon-delimited format, and write the result
to
tmp.txt
:
%
cvt_file.pl --idelim="\t" --odelim=":" data.txt > tmp.txt
The
cvt_file.pl
script has separate options for input and output. Thus, whereas
mysql_to_text.pl
has just a
--delim
option for specifying the column delimiter,
cvt_file.pl
has separate
--idelim
and
--odelim
options to set the input and output line
column delimiters. But as a shortcut,
--delim
is also supported to set the delimiter for
both input and output. The full set of options that
cvt_file.pl
understands is as follows:
--idelim=
str
,
--odelim=
str
,
--delim=
str
Set the column delimiter for input, output, or both. The option value can consist
of one or more characters.
--iquote=
c
,
--oquote=
c
,
--quote=
c
Set the column quote character for input, output, or both.
--ieol=
str
,
--oeol=
str
,
--eol=
str
Set the end-of-line sequence for input, output, or both. The option value can consist
of one or more characters.