LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-19-2007, 05:26 AM   #1
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
perl help with - if exists


Hi All,

I would like to know how to have
if else block
if am using
if exists to check whether an entity is available in hash

Code:
#! /opt/third-party/bin/perl
                                                                                
my(%fileHash);
                                                                                
open(FILE,"< $ARGV[0]") || die "Unable to open file $ARGV[0] <$!> \n";
                                                                                
while( chomp($_ = <FILE>) ) {
  $fileHash{$_}++;
}
                                                                                
close(FILE);
                                                                                
open(FILE,"< $ARGV[1]") || die "Unable to open file $ARGv[1] <$!> \n";
                                                                                
while(chomp($_ = <FILE>) ) {
  print "$_\n" if exists $fileHash{$_};
  ### How to have else condition to the above 'if exists' ###
}
                                                                                
close(FILE);
exit 0
Thanks in Advance
 
Old 01-19-2007, 06:00 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Instead of saying: print "$_\n" if exists $fileHash{$_};

You can also say:

if (exists $fileHash{$_}) { print "Yep, it exists!\n"; }

Which makes adding an else part easy:

Code:
if (exists $fileHash{$_}) {
  print "Yep, it exists!\n";
}
else {
  print "Nope, it doesn't exists\n";
}
Hope this helps.
 
Old 01-19-2007, 06:43 AM   #3
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Original Poster
Rep: Reputation: 30
Yes Definitely!

Thanks a lot for the help!

I should have the habit of structured learning.
 
  


Reply

Tags
perl


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To exists or not to exists, this is the Q. Inbal Linux - Newbie 3 07-18-2006 07:04 AM
Why do dentist exists? irfanhab General 24 07-13-2005 07:39 PM
SIOCADDRT: File exists SIOCCADDRT: File Exists Failed to bring up eth0. opsraja Linux - Networking 0 01-10-2005 09:29 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 09:09 PM
file exists? raven Programming 7 10-26-2003 08:44 AM


All times are GMT -5. The time now is 05:37 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration