Solved

Printing header just once ina a loop

Posted on 2012-04-09
3
198 Views
Last Modified: 2012-05-10
Hi i have a script that should look like this-
###########################################################
Plan: the plan its accessing
Chapter: the chapter under that plan
Owner:Default owner (there can be multiple owners)
Platfrom: Default platform
Env : Default environemt

(milestone=1 , env= if its not the default env display the other env here)
#print files relevant to milestone 1 under that plan under that chapter
filename1
filename2
filename3
filename4
(milestone=2 , env= if its not the default env display the other env here)
filename5
filename6
filename7
(milestone=3 , env= if its not the default env display the other env here)
filename8
(milestone=4 , env= if its not the default env display the other env here)
filename9
filename10
filename11
filename12
filename13
filename14
#############################################################

but instead looks like

############################################################
Plan: the plan its accessing
Chapter: the chapter under that plan
Owner:Default owner (there can be multiple owners)
Platfrom: Default platform
Env : Default environemt

(milestone=1 , env= if its not the default env display the other env here)
#print files relevant to milestone 1 under that plan under that chapter
filename1
filename2
filename3
filename4

Plan: the plan its accessing
Chapter: the chapter under that plan
Owner:Default owner (there can be multiple owners)
Platfrom: Default platform
Env : Default environemt

(milestone=2 , env= if its not the default env display the other env here)
filename5
filename6
filename7

Plan: the plan its accessing
Chapter: the chapter under that plan
Owner:Default owner (there can be multiple owners)
Platfrom: Default platform
Env : Default environemt

(milestone=3 , env= if its not the default env display the other env here)
filename8

Plan: the plan its accessing
Chapter: the chapter under that plan
Owner:Default owner (there can be multiple owners)
Platfrom: Default platform
Env : Default environemt

(milestone=4 , env= if its not the default env display the other env here)
filename9
filename10
filename11
filename12
filename13
filename14
##############################################################

So the body of my code looks like this
##############################################################
my $flag = 0;
foreach $plan(@plan){
      foreach $chap(@chap){
            foreach $mlst (@milestone){
                  foreach $plt (@platform){
                  for (i=0;i<=200;i++ )
                  if (($some{$hash}{$of}{plan} eq $plan) && ($some{$hash}{$of}{chap} eq $chap)&&($some{$hash}{$of}{mlst} eq $mlst)&&($some{$hash}{$of}{plt} eq $plt))
{
if ($flag ==0){
print "Plan= $plan";
print "Chapter=$chap";
print "Owner=$owner"; #processed in a subroutine
print "Platform=$plt";
print "(Milestone = $mlst , env: $en)";
}
print "$some{$hash}{$of}{filename}";
$flag = 1;
} } }  } }...
###########################################################

Please help me fix it !!
0
Comment
Question by:erum87
  • 2
3 Comments
 
LVL 12

Expert Comment

by:Amick
ID: 37824671
If I understand what you're trying to do this may work:

foreach $plan(@plan){
	print "Plan= $plan";
	foreach $chap(@chap){
		print "Chapter=$chap";
		print "Owner=$owner"; #processed in a subroutine
		print "Platform=$plt";
            foreach $mlst (@milestone){
		print "(Milestone = $mlst , env: $en)";
                  foreach $plt (@platform){
			  for (i=0;i<=200;i++ )
			  if (($some{$hash}{$of}{plan} eq $plan) && ($some{$hash}{$of}{chap} eq $chap)&&($some{$hash}{$of}{mlst} eq $mlst)&&($some{$hash}{$of}{plt} eq $plt))
				print "$some{$hash}{$of}{filename}";
			}
		}
	}
}

Open in new window

0
 

Author Comment

by:erum87
ID: 37824791
I'm sorry but it does not work :(

It prints the header for all the iterations its going through. And i just want it printed once at the top for that particular values relevant to those elements in the loop.
0
 
LVL 12

Accepted Solution

by:
Amick earned 500 total points
ID: 37829181
I'm sorry it didn't work for you.  This might be worth a try.

foreach $plan(@plan){
	if (($some{$hash}{$of}{plan} eq $plan)	print "Plan= $plan";
	foreach $chap(@chap){
	if (($some{$hash}{$of}{plan} eq $plan) && ($some{$hash}{$of}{chap} eq $chap){
		print "Chapter=$chap";
		print "Owner=$owner"; #processed in a subroutine
		print "Platform=$plt";
		}
            foreach $mlst (@milestone){
		if (($some{$hash}{$of}{plan} eq $plan) && ($some{$hash}{$of}{chap} eq $chap)&&($some{$hash}{$of}{mlst} eq $mlst) print "(Milestone = $mlst , env: $en)";
                  foreach $plt (@platform){
			  for (i=0;i<=200;i++ )
			  if (($some{$hash}{$of}{plan} eq $plan) && ($some{$hash}{$of}{chap} eq $chap)&&($some{$hash}{$of}{mlst} eq $mlst)&&($some{$hash}{$of}{plt} eq $plt))
				print "$some{$hash}{$of}{filename}";
			}
		}
	}
}

Open in new window

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

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…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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 tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

815 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

7 Experts available now in Live!

Get 1:1 Help Now