On Tue, 14 Jun 2005, Vijay Kumar Adhikari wrote:

> I have some tab delimited text data like
>
> ID    In      Out     Day
> 1     5       2       1
> 2     4       9       2
> 3     3       3       2
> 4     6       7       3
> 5     5       0       5
> 6     7       9       3
> 7     8       9       4
> 8     6       6       4
>
> I want to perform sorting by In, Out and so on. I also want to find
> sum of in out grouped by Day. Where should I start? Is there any
> module that may help me? I am a beginner.

My favorite CPAN module for this kind of work is DBD::CSV, because it
lets you use the DBI (DataBase Interface) module to write generic
database interaction programs that happen to use simple tab delimited
filees like this as the "database". In that way, the things you're
talking about doing become simple SQL statements, such as

    SELECT * FROM table ORDER BY In, Out

or (not as sure I have the syntax of this one right...)

    SELECT SUM(in), SUM(out) FROM table GROUP BY Day

Which is almost what you started with :-)

Another benefit of interacting with CSV data as if it's in a proper
database is that if in the future you choose to move your data into a
real database, your Perl code for interacting with that data should only
need minor changes: you substitute the DBD::CSV call for DBD::mysql or
whatever is appropriate, and you double-check that the SQL syntax is
correct for the database server you're switching to. Otherwise, things
shouldn't need much work to port your code from one server to another.

Learn more about DBD::CSV and DBI at:

    <http://search.cpan.org/~jzucker/DBD-CSV/lib/DBD/CSV.pm>
    <http://search.cpan.org/~timb/DBI/DBI.pm>

Or take a look at books like _Programming the Perl DBI_ or _Data Munging
With Perl_, which has a chapter or two on working with CSV data.


-- 
Chris Devers

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to