[問題] I/O 的問題
現在在練習寫一個情境,要使用者輸入id,密碼,email
然後把這寫資料寫入檔案裡
如果檔案裡已經有同樣的id名稱,要使用者重新輸入那些資料
一直遇到的問題是,我的程式一直無法偵測重覆的id
希望有好心人幫我看看我哪裡寫錯了,感激不盡
#!/usr/bin/perl
#-----------------------------------------------------
#Prompts the user for the userID, password and email
#Reprompt if userID already exist
#otherwise, append the input to the file
#------------------------------------------------------
$file="hw.out";
open(IN,$file) || die "can't read $file";
@all = <IN> ;
close(IN);
chomp @all;
%all= ();
foreach (@all) {
($id,$psw, $email) = split(/,/,$_);
$all{$id} = $pw;
print "Please enter your user ID:";
chomp ($id=<STDIN>);
print "Please enter your password:";
chomp ($psw=<STDIN>);
print "Please enter your email address:";
chomp ($email=<STDIN>);
if (exists ($all{$id}))
{
print "Please enter your user ID:";
chomp ($id=<STDIN>);
print "Please enter your password:";
chomp ($psw=<STDIN>);
print "Please enter your email address:";
chomp ($email=<STDIN>);
}
else {
open (IN, ">>$file") || die "can't write to $file";
print IN "$id, $psw, $email\n";
last;
}}
再次感謝好心人指教
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 149.159.132.73
討論串 (同標題文章)
Perl 近期熱門文章
PTT數位生活區 即時熱門文章