TSQL挑战题 - 第五题 - 20110711
时间:2011-07-11
来源:互联网
本帖最后由 rdcwayx 于 2011-07-11 14:33 编辑
这套TSQL挑战题其实是给SQL 管理员练手的, 我看着不错,只是做了简单的翻译,大家继续啊。
(你可以点上面的标签:TSQL挑战题, 可以找到所有的TSQL挑战题的帖子)
http://beyondrelational.com/blog ... ql-challenge-5.aspx
原文件:
1 post 1 sql,profiler,table
2 post 2 sql,performance
3 post 3 profilter
4 post 4 view,table
模板文件:
1 sql,performance
2 profiler
3 table,performance,view
要求的输出文件:
1 sql,performance post 2 sql,performance 2
1 sql,performance post 1 sql,profiler,table 1
2 profiler post 1 sql,profiler,table 1
3 table,performance,view post 4 view,table 2
3 table,performance,view post 1 sql,profiler,table 1
3 table,performance,view post 2 sql,performance 1
解释规则:
就是找出匹配,如果匹配了,就给出匹配的post名和匹配数。
这套TSQL挑战题其实是给SQL 管理员练手的, 我看着不错,只是做了简单的翻译,大家继续啊。
(你可以点上面的标签:TSQL挑战题, 可以找到所有的TSQL挑战题的帖子)
http://beyondrelational.com/blog ... ql-challenge-5.aspx
原文件:
1 post 1 sql,profiler,table
2 post 2 sql,performance
3 post 3 profilter
4 post 4 view,table
模板文件:
1 sql,performance
2 profiler
3 table,performance,view
要求的输出文件:
1 sql,performance post 2 sql,performance 2
1 sql,performance post 1 sql,profiler,table 1
2 profiler post 1 sql,profiler,table 1
3 table,performance,view post 4 view,table 2
3 table,performance,view post 1 sql,profiler,table 1
3 table,performance,view post 2 sql,performance 1
解释规则:
就是找出匹配,如果匹配了,就给出匹配的post名和匹配数。
作者: rdcwayx 发布时间: 2011-07-11
规则没看懂,怎么算匹配,最后的匹配数怎么算出来的?
作者: ly5066113 发布时间: 2011-07-11
本帖最后由 yinyuemi 于 2011-07-11 14:55 编辑
gawk 4.0.0
array of array
复制代码
gawk 4.0.0
array of array
- awk -F'[ ,]+' '{for(i=4;i<=NF;i++)a[$2" "$3][$i]=gensub(/^. +/,"",1,$0)}END{while (getline line <"file-B") {for(i in a) {x=0;for(j in a[i]) if(index(line,j)){x++};if(x>0)print line"\t"a[i][j]"\t"x}} }' file-A
- 1 sql,performance post 1 sql,profiler,table 1
- 1 sql,performance post 2 sql,performance 2
- 2 profiler post 1 sql,profiler,table 1
- 3 table,performance,view post 1 sql,profiler,table 1
- 3 table,performance,view post 2 sql,performance 1
- 3 table,performance,view post 4 view,table 2
作者: yinyuemi 发布时间: 2011-07-11
QUOTE:
规则没看懂,怎么算匹配,最后的匹配数怎么算出来的?
ly5066113 发表于 2011-07-11 14:35
ly5066113 发表于 2011-07-11 14:35
比如模板 1 sql,performance
在原文件中,sql,performance 出现在post1 和post2 ,只要有一个匹配都算。 sql,performance 在post2 中被匹配,就输出2, 但在post1 中只有一个匹配(sql),就输出1.
作者: rdcwayx 发布时间: 2011-07-11
0> perl 1.pl|sort
1 sql,performance post 1 sql,profiler,table 1
1 sql,performance post 2 sql,performance 2
2 profiler post 1 sql,profiler,table 1
3 table,performance, view post 1 sql,profiler,table 1
3 table,performance, view post 2 sql,performance 1
3 table,performance, view post 4 view,table 2
1.pl
use strict;
use warnings;
my %HASH_TEM = (
1 => "sql,performance",
2 => "profiler",
3 => "table,performance, view",
);
while ( my $line = <DATA> ) {
chomp $line;
my ( $post, $str ) = ( split( '\t', $line ) )[ 1, 2 ];
foreach my $key ( keys %HASH_TEM ) {
my $re = $HASH_TEM{$key};
$re =~ s/\s+//g;
$re =~ s/,/\|/g;
if ( $str =~ m#$re# ) {
my $num = () = $str =~ m#$re#g;
print "$key\t$HASH_TEM{$key}\t$post\t$str\t", $num, "\n";
}
}
}
__DATA__
1 post 1 sql,profiler,table
2 post 2 sql,performance
3 post 3 profilter
4 post 4 view,table
没有排序
1 sql,performance post 1 sql,profiler,table 1
1 sql,performance post 2 sql,performance 2
2 profiler post 1 sql,profiler,table 1
3 table,performance, view post 1 sql,profiler,table 1
3 table,performance, view post 2 sql,performance 1
3 table,performance, view post 4 view,table 2
1.pl
use strict;
use warnings;
my %HASH_TEM = (
1 => "sql,performance",
2 => "profiler",
3 => "table,performance, view",
);
while ( my $line = <DATA> ) {
chomp $line;
my ( $post, $str ) = ( split( '\t', $line ) )[ 1, 2 ];
foreach my $key ( keys %HASH_TEM ) {
my $re = $HASH_TEM{$key};
$re =~ s/\s+//g;
$re =~ s/,/\|/g;
if ( $str =~ m#$re# ) {
my $num = () = $str =~ m#$re#g;
print "$key\t$HASH_TEM{$key}\t$post\t$str\t", $num, "\n";
}
}
}
__DATA__
1 post 1 sql,profiler,table
2 post 2 sql,performance
3 post 3 profilter
4 post 4 view,table
没有排序
作者: magnet2008 发布时间: 2011-07-11
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28