in reply to Problem with sub
use strict; # do this all the time addbuddy("Peter1"); # the more test cases, the better addbuddy("Peter2"); addbuddy("Peter1"); addbuddy("Peter3"); addbuddy("peTer 1"); sub addbuddy { my $victim = shift; $victim =~ s/ //g; # transform victim in the same way you transfor +m the list, otherwise you may run into trouble without your notice $victim = lc($victim); open(FILE, "<", "names.txt"); # I don't want use +< to confuse you +, even though it is better for this case my @list = <FILE>; #read in the whole list close(FILE); foreach my $item (@list) { chomp($item); #get rid of line breaks $item =~ s/ //g; $item = lc($item); } my %list = map {$_, 1} @list; # the only thing looks tricky in my +code if (!defined($list{$victim})) { open (DATA, ">>names.txt");# you don't want to use >, and over +write your file. print DATA "$victim\n"; close (DATA); print "Buddy $victim was added\n";# always make the debug msg +meaningful } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Problem with sub
by Cywiz (Initiate) on Dec 29, 2002 at 09:35 UTC | |
by davorg (Chancellor) on Dec 29, 2002 at 13:00 UTC |