Solved

Perl - foreach loop and else statement

Posted on 2010-09-06
3
994 Views
Last Modified: 2012-05-10
I have the foreach loop in a sub I use, I cannot get the "else" to print if $match is not found, the way I have it below works ok other than I cannot get the  print to work for the else.

Any help would be much appreciated.

foreach my $regions (@regions) {
 open(GCSEA,"/usr/local/bin/adsearch xxx samaccountname=$sam-$regions samaccountname | ");
  while(<GCSEA>) {
   if (/^sAMAccountName: (.*?)/) {
    (my $match) = (/sAMAccountName: (.*)/);
if (defined($match)) {
   print "\t$regions-",$match,"\t";
} else {
   print $regions,"-NotInAd\n";
}
   } 
 }
}

Open in new window

0
Comment
Question by:bt707
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 65

Accepted Solution

by:
rockiroads earned 300 total points
ID: 33613062
get your indentation sorted, it will make life easier to read

Now looking at your code, have you checked the first if? there is no else on that. So your code is only going to print something if it passed the first if condition
Can you confirm it is going pass the first if?

the attached code has been indented properly with the extra else added on the first if

foreach my $regions (@regions) {
	open(GCSEA,"/usr/local/bin/adsearch xxx samaccountname=$sam-$regions samaccountname | ");
	while(<GCSEA>) {
		if (/^sAMAccountName: (.*?)/) {
			(my $match) = (/sAMAccountName: (.*)/);
			if (defined($match)) {
				print "\t$regions-",$match,"\t";
			} else {
				print $regions,"-NotInAd\n";
			}
		} else {
			print "Dodgy\n";
		}
	}
}

Open in new window

0
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 200 total points
ID: 33614609
Note that both your first and second if statement are looking for the same thing.  So, if it matches the first time, it will match the second time - and you'll never get to the else for the second if.  In rockiroads code, this would print "Dodgy".

0
 

Author Closing Comment

by:bt707
ID: 33617490
Thanks to both for the help, I figured out why I cannot get what I want, it is basically in the first condition and also in the way I put in the if/else statements which both pointed out. I got it working now.

Thanks,
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…

740 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