[問題] 資料庫欄位內容與txt檔案內容比對

看板Perl作者 (小那)時間14年前 (2010/10/05 18:06), 編輯推噓2(202)
留言4則, 2人參與, 最新討論串1/1
大家好 我是新手,我才自學一個月而已, 請鞭小力一點 我想要作一個功能,把txt裡面一個字串,跟資料庫某個欄位作比較 目前最迫切要做到這點~~~>"< 以下程式中 @stud_ans 是我開檔去讀一個txt,把裡面的字串變成一個陣列 $hash_ref->{'Answer'}; 就是我要比對的資料庫攔位, 正確的說,這個欄位存著標準答案 學生的答案,要跟標準答案作比較,往後再作一些正規表示法的比較 問題是,我根本沒辦法直接拿 $hash_ref->{'Answer'} 去作比較 感覺他好像關在while loop裡面 出去外面大家就不認識他了 請問要怎麼拿出來比較呢???? 這是我的網址 http://140.116.39.115/cgi-bin/ans.pl ============分隔線=========以下是程式碼======================== #!c:\perl\bin\perl.exe print "Content-type: text/html\n\n"; $file ='c:\\Program Files\\Java\\jdk1.6.0_21\\bin\\stanford-parser-2008-10-26 \\student_output.txt'; open(STUDENT_ANS,$file); @stud_ans =<STUDENT_ANS>; close(STUDENT_ANS); print @stud_ans; #以上是我開txt讀出學生的答案,一定要讀txt,這它的應用程式綁住了 #以下我開始連MYSQL #想讀出 answer 欄位 use DBI; use strict; my $dbh; if ($dbh=DBI->connect("DBI:mysql:gram_sys","root","XXXXX0") ) { print "Connect Database:test OK!\n"; } else { print "can't connect database:test!"; exit(0); } #check connect print " <br> \n"; my $sql = "SELECT * FROM item_set WHERE SN=32"; my $sth = $dbh->prepare($sql); $sth->execute; while (my $hash_ref = $sth->fetchrow_hashref) { print $hash_ref->{'SN'}; print " <br> \n"; print $hash_ref->{'Answer'}; print $hash_ref->{'Question'}; }##end of while $sth->finish; $dbh->disconnect; exit; -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.116.39.115 ※ 編輯: nana0130 來自: 140.116.39.115 (10/05 18:11)

10/05 19:11, , 1F
用了my等於宣告了他是這個block的私有變數
10/05 19:11, 1F

10/05 19:14, , 2F
所以你可以在block外先宣告my $hash_ref,
10/05 19:14, 2F

10/05 19:15, , 3F
或是while expression裡不要加my 讓他變全域變數
10/05 19:15, 3F

10/05 21:17, , 4F
喔,謝謝你~~
10/05 21:17, 4F
文章代碼(AID): #1CglYkdK (Perl)
文章代碼(AID): #1CglYkdK (Perl)