|
Data::Describe - Perl extension for scanning/describing a text file or array. |
Data::Describe - Perl extension for scanning/describing a text file or array.
use Data::Describe;
$dsp = Data::Describe->new; # create an empty object
my %arg = ( input_file_name => 'input.txt', # the same as 'ifn'
skip_first_row => 'Y', # the same as 'sfr'
input_field_sep => ',', # the same as 'ifs'
ofs=>'|', # the same as 'output_field_sep'
ofn=>'out.dat', # the same as 'output_file_name'
odf=>'out.def', # the same as 'output_def_file'
);
$dsp = Data::Describe->new(%arg); # with arguments
$dsp->skip_first_row; # i,e. 1st row contains col names $dsp->set_sfr(1); # is the same as the above
$dsp->set_ifs('\t'); # set input field separator to tab
$dsp->input_field_separator('|'); # set input field separator to '|'
$dsp->set_ofs('|'); # set output field separator to |
$dsp->output_field_separator('|');# set output field separator to |
$dsp->set_ifn('input.txt'); # set input file name
$dsp->input_file_name('input.txt'); # set input file name
$dsp->input_file_name($arf); # it can be array ref
$dsp->set_ofn('out.dat'); # set output file name
$dsp->output_file_name('out.dat');# set output file name
$dsp->output_file_name('Y'); # it can be array ref
$dsp->set_odf('out.def'); # set output def file name
$dsp->output_def_file('output.def');# set output definition file name
$dsp->output_def_file('Y'); # default to '${in}.def"
# all the set method has its corresponding get method $rc = $dsp->get_sfr; $rc = $dsp->get_ifs; $rc = $dsp->input_field_separator; # the same as get_ifs
$dsp->debug(5); # set debug level to 5
$dsp->echoMSG('This message', 1); # tag the message as level 1
my $crf = $dsp->get_def_arrayref;
my $drf = $dsp->get_dat_arrayref;
$dsp->output($crf, "", 'def'); # output def file to STDOUT
$dsp->outptu($drf, 'out.dat', 'dat');
This class contains a describe method that scans through each records or number of records sepcified and fields in those records in the array or a file to collect information about the content in the array or the file. It creates a column definition array and a data array containing all the data without the column record.
The column definition array built by the module is actually an array with hash members. It contains these hash elements ('col', 'typ', 'max', 'min', 'dec', 'req' and 'dsp') for each column. The subscripts in the array are in the format of $ary[$col_seq]{$hash_ele}. The hash elements are:
col - column name
typ - column type, 'N' for numeric, 'C' for characters,
and 'D' for date
max - maximum length of the records in the column
(could use 'wid' to record the max length of the
records.)
min - minimum length of the record in the column
(When 'wid' is used, no 'min' is needed.)
dft - date format such as YYYY/MM/DD, MON/DD/YYYY, etc.
dec - maximun decimal length of the record in the column
req - whether there is null or zero length records in the
column only 'NOT NULL is shown
dsp - description of the columns
The array or records passed to the module can have the first row containing column names.
This class contains many methods to ``set'' and/or ``get'' parameters. Here is the list of methods:
new(%arg)describe($inf,$def,$out,$sfr,$ifs,$ofs,$nrc,$owt,$chr)
$inf - input file name, full path to a ASCII file
$def - output file name for column definitions,
default to "*.def" while $def = undef or 'Y'
$out - output file name for data,
default to "*.dat" while $out = 'Y'
$sfr - skip first row, i.e., the first row contains
column names
$ifs - input field separator, default is '|'
$ofs - output field separator, default is '|'
$nrc - first number of lines to be read,
default is to read all
$owt - overwrite existing files, default to 'N'
$chr - quote characters to be removed.
echoMSG - print debug messages
use Data::Describe; my $dsb= Data::Describe->new; my ($crf,$drf) = $dsp->describe($inf,'Y','',$sfr);
This routine reads in a text file, search its content and create column definitons. The $crf contains the column definiton, i.e., ${$crf}[$j]{$itm}, where $j is column sequence and $itm includes: 'col', 'typ', 'wid', 'dec', 'dft', 'dsp', etc.
The $drf contains the data, ${$drf}[$i][$j], where $i is record number and $j is column name. The first row contains column names. The rest rows are data.
output($arf,$out,$otp,$ifn,$ofs,$owt)$arf - array ref $out - output file name $otp - output type: data or definition $ifn - input file name as name reference $ofs - output field separator $owr - whether to overwrite existing file
get_def_arrayref - get column definition array reference get_dat_arrayref - get data array reference get_odf - get definition output file name get_ofn - get data output file name get_ofs - get output field separator get_ifn - get input file name fileparse - parse file name
my $crf = $self->get_def_arrayref; # output $crf to standard output device - STDOUT $self->output($crf, "", 'def'); my $drf = $self->get_dat_arrayref; # output $drf to 'out.dat' $self->output($drf, "out.dat", 'dat');
debug($n)
$n - a number between 0 and 100. It specifies the
level of messages that you would like to
display. The higher the number, the more
detailed messages that you will get.
$self->debug(2); # set the message level to 2 print $self->debug; # print current message level
$msg - the message to be displayed. No newline
is needed in the end of the message. It
will add the newline code at the end of
the message.
$lvl - the message level is assigned to the message.
If it is higher than the debug level, then
the message will not be displayed.
$yn - whether to return the message
debug - get debug level.
# default msg level to 0
$self->echoMSG('This is a test");
# set the msg level to 2
$self->echoMSG('This is a test", 2);
This method will display message or a hash array based on debug level. If debug is set to '0', no message or array will be displayed. If debug is set to '2', it will only display the message level ($lvl) is less than or equal to '2'. If you call this method without providing a message level, the message level ($lvl) is default to '0'. Of course, if no message is provided to the method, it will be quietly returned.
This is how you can call echoMSG:
my $df = Data::Describe->new;
$df->echoMSG("This is a test"); # default the msg to level 0
$df->echoMSG("This is a test",1); # assign the msg as level 1 msg
$df->echoMSG("Test again",2); # assign the msg as level 2 msg
$df->echoMSG($hrf,1); # assign $hrf as level 1 msg
$df->echoMSG($hrf,2); # assign $hrf as level 2 msg
If debug is set to '1', all the messages with default message levels 0 and 1 will be displayed. The higher level messages will not be displayed.
$r1 - date range 1: 'min:max' $r2 - date range 2: 'min:max' $r3 - date range 3: 'min:max' $ds - date separator
Variables used or routines called:
None.
How to use:
# the $dft = 'MM/DD/YY'
my $dft = $self->get_date_format('1:12','1:31','1:2');
# the $dft = 'MM/DD/YYYY'
$dft = $self->get_date_format('1:12','1:31','0:2002');
Return: the date format.
You can create a describe object as the following:
$dsc = Data::Describe->new; # an empty object
You can set a hash to define your object attributes and create it as the following:
%attr = (
input_field_sep => ':', # output field separator
skip_first_row' => 1, # 1st row has col names
);
$dsp = Data::Describe->new(%attr);
If the first row in the data array contains column names, it uses the column names in the row to define the column definition array. The column type is determined by searching all the records in the data array. If all the records in the column only contain digits, i.e., only [0-9.], the column is defined as numeric ('N'); otherwise, it is defined as character ('C'). In type 'C', it checks whether the string is a date type. If the field only contains digits and '/', then it consider the field as a date field. It calls to get_date_foramt to determine the date format.
If the first row does not contain column names, it will generate field names as ``FLD###''. The ``###'' is a sequential number starting with 1. If the minimum length of a column is zero, then the value in the column can be null; if the minimum length is greater than zero, then it is a required column.
The default indicator for the first row is false, i.e., the first row does not contain column names. You can indicate whether the first row in the data array is column names by using skip_first_row or set_sfr to set it.
$dsp->skip_first_row('Y'); # first row contains column names
$dsp->set_sfr('Y'); # the same as the above
$dsp->set_sfr(1); # the same as the above
To reverse it, here is how to
$dsp->set_sfr('N'); # no column in the first row
$dsp->skip_first_row(0); # the same as the above
Although it seems a simple task, it requires a lot of thinking to get it working in an object-oriented frame. Intented future implementation includes
You are welcome to give me suggestions.
Hanming Tu, hanming_tu@yahoo.com
perltoot(1), perlobj(1), perlbot(1), perlsub(1), perldata(1), perlsub(1), perlmod(1), perlmodlib(1), perlref(1), perlreftut(1).
|
Data::Describe - Perl extension for scanning/describing a text file or array. |