Question : check if user exists in hash - what am I doing wrong?

Hi

I made this quick scripts to check if the users in a file are loaded into a hash.

this is the file: (txt file)
quintus
renee
usera
userb

and this is the script:

$capfile = "path to file";

 my $x = 0;
 open (CAPPED, $capfile) || die "Can't open file $capfile: $!";
   while (my $line = <CAPPED>) {
      $caplist{$line} = $x;
      $x ++ ;
   }
 close(CAPPED);

if (exists $caplist{"renee"}) {
 print "true";
 }


Why am i not getting a "true"?

Answer : check if user exists in hash - what am I doing wrong?

You are not removing the trailing end of line \n characters from your input file that you put into $line so "renee" does not exist in your hash but "renee\n" would.

You can chomp the input from your text file to clear this up like  below which should do the trick.

  while (my $line = <CAPPED>) {
      chomp $line;
      $caplist{$line} = $x;
      $x ++ ;
   }
Random Solutions  
 
programming4us programming4us