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

Posted on 2010-08-24
Medium Priority
Last Modified: 2012-05-10

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

this is the file: (txt file)

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 ++ ;

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

Why am i not getting a "true"?
Question by:QuintusSmit
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

deiaccord earned 500 total points
ID: 33508791
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 ++ ;

Author Comment

ID: 33508804
ahh - thank you

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question