作者yingwan (yingwan)
看板Perl
标题[问题] I/O 的问题
时间Wed Oct 8 08:24:49 2008
现在在练习写一个情境,要使用者输入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