LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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, 04: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, 05:00 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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, 05: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



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 06:04 AM
Why do dentist exists? irfanhab General 24 07-13-2005 06:39 PM
SIOCADDRT: File exists SIOCCADDRT: File Exists Failed to bring up eth0. opsraja Linux - Networking 0 01-10-2005 08:29 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
file exists? raven Programming 7 10-26-2003 07:44 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:36 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration