Solved

Perl - foreach loop and else statement

Posted on 2010-09-06
3
949 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
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

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 have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

757 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

22 Experts available now in Live!

Get 1:1 Help Now