in reply to Problem with sub

Here is some code for you, and I have comments along with it.

Two suggestions:
  1. use strict all the time
  2. always test with -w
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
    thanks to you all. but it adds the $victim like this to the file net::aim=hash(0x194efc4) instead of botroktester but it will send replies to that name so it is working just want to know why and if that could be changed?

      That's a problem with whatever you're passing into the subroutine. Your subroutine seems to be expecting a string that contains a name, but you're actually passing in a Net::Aim object.

      --
      <http://www.dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg