Solved

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

Posted on 2010-08-24
2
228 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
Comment Utility
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
Comment Utility
ahh - thank you
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

744 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

15 Experts available now in Live!

Get 1:1 Help Now