我試圖在命令行末尾添加一個參數,通過MySQL數據庫運行搜索,然後列出結果或說什麼都沒找到。我試圖通過將查詢數據保存爲哈希和數組來做到這些(這些是練習,我對PERL非常陌生,腳本和嘗試學習)。但是,我無法弄清楚如何使用散列做同樣的事情。我希望SQL查詢完成,然後將輸出寫入散列,以免調用While函數。任何指導將不勝感激。將Perl SQL查詢的輸出保存爲散列而不是數組
#!/usr/bin/perl -w
use warnings;
use DBI;
use Getopt::Std;
&function1;
&function2;
if ($arrayvalue != 0) {
print "No values found for '$search'"."\n"};
sub function1 {
getopt('s:');
$dbh = DBI->connect("dbi:mysql:dbname=database", "root", "password")
or die $DBI::errstr;
$search = $opt_s;
$sql = $dbh->selectall_arrayref(SELECT Player from Players_Sport where Sport like '$search'")
or die $DBI::errstr;
@array = map { $_->[0] } @$sql;
$dbh->disconnect
or warn "Disconnection failed": $DBI::errstr\n";
}
sub function2 {
@array;
$arrayvalue=();
print join("\n", @array, "\n");
if(scalar (@array) == 0) {
$arrayvalue = -1
}
else {$arrayvalue = 0;
};
}
完美奏效,謝謝。 – user2361820 2013-05-10 04:54:46