作者giacch (小a)
看板Perl
标题Re: [问题] I/O 的问题
时间Wed Oct 8 09:25:40 2008
※ 引述《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
1F:→ giacch:$file="hw.out"; # hw等於homework? 作业要自己... XD 10/08 09:32
2F:→ yingwan:是作业啦..我想了一天都想不出来上来问人的,很不好意思, 10/08 12:52
3F:→ yingwan:谢谢g大啦 10/08 12:54