Re: [問題] I/O 的問題

看板Perl作者 (小a)時間15年前 (2008/10/08 09:25), 編輯推噓0(003)
留言3則, 2人參與, 最新討論串2/2 (看更多)
※ 引述《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
$file="hw.out"; # hw等於homework? 作業要自己... XD
10/08 09:32, 1F

10/08 12:52, , 2F
是作業啦..我想了一天都想不出來上來問人的,很不好意思,
10/08 12:52, 2F

10/08 12:54, , 3F
謝謝g大啦
10/08 12:54, 3F
文章代碼(AID): #18x0mLnV (Perl)
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):
文章代碼(AID): #18x0mLnV (Perl)