Solved

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

Posted on 2010-08-24
2
230 Views
Last Modified: 2012-05-10
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"?
0
Comment
Question by:QuintusSmit
2 Comments
 
LVL 6

Accepted Solution

by:
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 ++ ;
   }
0
 
LVL 1

Author Comment

by:QuintusSmit
ID: 33508804
ahh - thank you
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PERL - Find newest folder 12 139
perl: Cleaning meta tags using RegEX 12 81
Perl Untar File 1 45
Automate the blocking off IP's from a list file in Ubuntu 9 20
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

823 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