Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
This module is used for reading character separated data. At the
suggestion of it's creator, I used it in a project of mine,and I am
quite glad that I did so.

Here are the available methods within this module:

This is the constructor. It will take a hash of three arguments, all of which are optional. The file name, file handle, and the single character separator are the three arguments. If a filename is passed, and a file handle isn't, it will open a filehandle on that file and set the filehandle accordingly. Also, the separator value is a comma by default.

These are the set methods for the optional arguments for the new() constructor.

This method takes the name of a file and opens it. It will also set the filename
and file handle.

Takes an array of field names and memorizes the positions for later use.

bind_headers() Reads a row from the file as a header line, and memorizes the field positions for
later use. This method is preferred over the bind_fields method.

Reads row from file and returns an array or a reference to an array, depending
upon context. It also stores the row in the row property for later use.

Extracts a list of fields out of the last row read.

The Good
As tilly points out in the POD that accompanies the module, most people try to use
split to separate value separated lines, or they read the line and try to parse it. This
makes it impossible to handle returns that are embedded in a field.
This problem is solved by the creation of the xSV object with access to the filehandle, and
if in parsing, it notices that a new line is needed, it can read the file at will.

The Bad (and Ugly)
The module is very unforgiving concerning the character separator. It only works on a single
character as a separator. The overall speed isn't to bad, but like in all things, there is always
room for improvement.

The below section of code is taken from my program. This function
is what reads all of the questions into a hash reference, where I later pull
questions from it at random.
sub Loader{ my $Data = shift; my $file = shift; ## I declare the Text::xSV object my $XSV = Text::xSV->new(); my $question_number; my $length; my $f; my @Sorter=(); ## I set my separator here, otherwise it ## defaults to a comma. $XSV->set_sep("|"); open(FH,"$file")||die "Can't open $file: $!\n"; while(<FH>){ ## Here I get use the get_row method to retrieve ## the row, which also parses it. @Sorter = $XSV->get_row(); $question_number = shift @Sorter; $length = @Sorter; for($f = 0;$f < $length;$f++){ $Data{$question_number}[$f] = $Sorter[$f]; }#End of for loop }#End of while loop close FH; return $Data; }#End of sub
One thing I did notice is that if the lines were of different lengths (If one question was multiple choice, and the next was a true/false question), the module would spit out a warning to that effect, the information was also stored in the row property,and it noticed the difference. This, however, did not affect the overall performance of the program itself.

In reply to Text::xSV by TStanley

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2023-12-08 00:40 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (33 votes). Check out past polls.