首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

求解,没弄过perl,有个perl面试题解决方法

2012-05-02 
求解,没弄过perl,有个perl面试题Given a table mailing:CREATE TABLE mailing (addr VARCHAR(255) NOT N

求解,没弄过perl,有个perl面试题
Given a table 'mailing':

CREATE TABLE mailing (
addr VARCHAR(255) NOT NULL
);

The mailing table will initially be empty. New addresses will be added on a daily basis. It is expected that the table will store at least 10,000,000 email addresses and 100,000 domains.

Write a perl script that updates another table which holds a daily count of email addresses by their domain name.

Use this table to report the top 50 domains by count sorted by percentage growth of the last 30 days compared to the total.

** NOTE **
- You MUST use the provided DB.pm for all database interaction, and you must use it as it is (DB.pm cannot be modified except for the connection settings).

- The original mailing table should not be modified.

- All processing must be done in Perl (eg. no complex queries or sub-queries)

- Submit a compressed file(tar/zip) with the files required to run your script.

--------------------DB.pm-------------------------
package GUI::DB;

use strict;
use DBI;

use vars qw(@ISA @EXPORT);
use Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(dbConnect query);

#
# dbConnect - connect to the database, get the database handle
#
sub dbConnect {

? # Read database settings from config file:
? my $dsn = "DBI:mysql:database=test";
? my $dbh = DBI->connect( $dsn,
'',
'',
? { RaiseError => 1 }?
);

? return $dbh;

}

#
# query - execute a query with parameters
# query($dbh, $sql, @bindValues)
#
sub query {
? my $dbh = shift;
? my $sql = shift;
? my @bindValues = @_; # 0 or serveral parameters

? my @returnData = ();

? # issue query
? my $sth = $dbh->prepare($sql);

? if ( @bindValues ) {
? $sth->execute(@bindValues);
? } else {
? $sth->execute();
? }

? if ( $sql =~ m/^select/i ) {
? while ( my $row = $sth->fetchrow_hashref ) {
? push @returnData, $row;
? }
? }

? # finish the sql statement
? $sth->finish();

? return @returnData;
}

__END__


[解决办法]
把面试题放到这上面求教,不厚道吧

用SQL从第一个表中获取top 50 的domain,然后用Perl写到另一张表中就是了~~~
[解决办法]
搞错了一点,应该是先获取第一个表中所有的数据,然后用Perl做个排序,在存到新表中去~~~

热点排行