生成搜索query
发布时间:2020-12-15 21:08:07 所属栏目:大数据 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 #!/usr/bin/perl -wuse strict;#==声明变量============================================================my $fieldList; #字段列表my $queryFile; #
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 #!/usr/bin/perl -w use strict; #==声明变量============================================================ my $fieldList; #字段列表 my $queryFile; #query文件 my $field; #字段变量 my @arr; my $arr; my $index; my $queryStr = ""; my $queryCount; my $l = 1; my $area; #==逻辑处理=========================================================== if(@ARGV < 3) { print "Execute error!n"; print "Usage: ./queryGen.pl fieldList queryCountn"; exit(1); } else { $fieldList = $ARGV[1]; $queryCount = $ARGV[2]; $area = $ARGV[0]; $queryFile = "$fieldList.query"; open(FILEOUT,">>$queryFile") || die "can't creat the file:$!n"; while($l <= $queryCount) { open(FILEIN,"<$fieldList" ) || die "can't open the file:$!n"; while($field = <FILEIN>) { chomp($field); @arr = split(/:/,$field); if($arr[1] eq 'v') { $queryStr = $queryStr."&$arr[0]=$arr[2]"; } elsif($arr[1] eq 'a') { my @valarr = split(/,/,$arr[2]); my $val = &getRandItem(@valarr); $queryStr = $queryStr."&$arr[0]=$val"; } elsif($arr[1] eq 'f') { my $fileLineNum = &countFileLine($arr[2]); #chomp($fileLineNum); #print "The line number is $fileLineNumn"; my $fileLine = int(rand($fileLineNum)) + 1; chomp($fileLine); #print "The rand line is $fileLinen"; my $fileItem = `sed -n '$fileLine''p' $arr[2]`; chomp($fileItem); #print "File item is $fileItemn"; $queryStr = $queryStr."&$arr[0]=$fileItem"; } } $queryStr = substr($queryStr,1); print FILEOUT "bin/search?$area?$queryStr"; $queryStr = ""; close(FILEIN); $l++; print FILEOUT "n"; } close(FILEOUT); } #====================================================== sub countFileLine { my $line = 0; my($path) = @_; open(FD,$path); while(<FD>) { $line++; } return $line; } #生成不定长度的随机数 sub getRandNum { my $maxLenth; my @a; my @c; my $count; my $password; #随机串的内容 @c=(1..2); #生成随机串 $count = join '',map { $c[int rand @c] } 0..(0); #0..(0)限制随机串的长度 #第一个随机串作为下一个随机串的长度 $maxLenth=$count; #第二个随机串的内容 @a=(1..9); #生成最终的随机串 $password = join '',map { $a[int rand @a] } 0..($maxLenth-1); #返回字串 return $password; } #从数组中随机取字符或字串 sub getRandStr { #字串的内容 my @a = ("Y","N"); #字串的随机位置 my @c = (0..1); my $i = join '',map { $c[int rand @c] } 0..(0); #根据随机位置取随机字符 return $a[$i]; } #从任意数组中随机取item sub getRandItem { my @array = @_; my $count = scalar(@array) - 1; #my $i = join '',map { $c[int rand @c] } 0..(0); my $r = int(rand($count)); return $array[$r]; } 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |