Re: [問題] I/O 的問題
※ 引述《yingwan (yingwan)》之銘言:
: 現在在練習寫一個情境,要使用者輸入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;
: }}
: 再次感謝好心人指教
#!/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;
$all{$id} = $psw;
}
#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}))
do
{
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>);
#}
} while(exists $all{$id});
#else {
#open (IN, ">>$file") || die "can't write to $file";
open (OUT, ">>$file") || die "can't write to $file";
#print IN "$id, $psw, $email\n";
print OUT join(',', $id, $psw, $email) . "\n";
close(OUT);
#last;
#}}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.232.236.185
→
10/08 09:32, , 1F
10/08 09:32, 1F
→
10/08 12:52, , 2F
10/08 12:52, 2F
→
10/08 12:54, , 3F
10/08 12:54, 3F
討論串 (同標題文章)