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

Posted on 2010-08-24
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

Accepted Solution

deiaccord earned 125 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

Network it in WD Red

There's an industry-leading WD Red drive for every compatible NAS system to help fulfill your data storage needs. With drives up to 8TB, WD Red offers a wide array of solutions for customers looking to build the biggest, best-performing NAS storage solution.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error when calling SSH command 20 108
Linux : can't create transaction lock error 1 59
Perl string filter 5 74
iptables limit connection per ip correct way ? 2 96
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 …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now