For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > November 2007 > help in if - else loop in perl









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author help in if - else loop in perl
Irfan Sayed

2007-11-27, 8:00 am

Hi All,

I am getting error while running following script.Error is due to
improper if else loop and curly braces.

Can you please guide me in placing the braces in right position and
doing proper if..else looping

My requirement is that I want to quit as soon as "if condition"
fails.Please guide

if ($menu_item == 1) (if this condition fails then i want to quit)
{
$pvob=`$CT lsvob -s | grep apc`;
chomp($pvob);
my @proj=`$CT lsproj -invob $pvob | cut -d " " -f3`;
$len=@proj;
print "The projects are.....\n\n";
my $i=1;
foreach my $line(@proj)
{
print "\t" . $i++ . ". $line\n";
}
print "Your Choice :\t";
chomp($choice = <STDIN> );
if ($choice < $len){ (if this condition fails then i want to quit)
print "Your choice: $proj[$choice-1]\n";
chomp($pro=$proj[$choice-1]);
chomp($proj1=$pro . "@" . $pvob);
#print "$proj1\n";

my $int_str=`$CT desc -fmt "%[istream]p" project:$proj1`;}
else {quit();}
elsif ($int_str eq "") { print "This project does not have
Integration stream , so quiting.....\n"; exit; }(if this condition fails
then i want to go to following else loop)
else{
print "Integration Stream:$int_str\n";
dev_strm();}

}
else {quit();}

Regards
Irfan



Klaus Jantzen

2007-11-27, 8:00 am

Sayed, Irfan (Irfan) wrote:
> Hi All,
>
> I am getting error while running following script.Error is due to
> improper if else loop and curly braces.
>
> Can you please guide me in placing the braces in right position and
> doing proper if..else looping
>
> My requirement is that I want to quit as soon as "if condition"
> fails.Please guide
>
> if ($menu_item == 1) (if this condition fails then i want to quit)
> {
> $pvob=`$CT lsvob -s | grep apc`;
> chomp($pvob);
> my @proj=`$CT lsproj -invob $pvob | cut -d " " -f3`;
> $len=@proj;
> print "The projects are.....\n\n";
> my $i=1;
> foreach my $line(@proj)
> {
> print "\t" . $i++ . ". $line\n";
> }
> print "Your Choice :\t";
> chomp($choice = <STDIN> );
> if ($choice < $len){ (if this condition fails then i want to quit)
> print "Your choice: $proj[$choice-1]\n";
> chomp($pro=$proj[$choice-1]);
> chomp($proj1=$pro . "@" . $pvob);
> #print "$proj1\n";
>
> my $int_str=`$CT desc -fmt "%[istream]p" project:$proj1`;}
> else {quit();}
> elsif ($int_str eq "") { print "This project does not have
> Integration stream , so quiting.....\n"; exit; }(if this condition fails
> then i want to go to following else loop)
> else{
> print "Integration Stream:$int_str\n";
> dev_strm();}
>
> }
> else {quit();}
>
> Regards
> Irfan
>
>
>
>

You can't have an "else" in front of an "elsif" (RTFM).

--

K. Jantzen

Tel.: +49-{0}7034-929651
Fax: +49-{0}7034-929652

Matthew Whipple

2007-11-27, 8:00 am

Sayed, Irfan (Irfan) wrote:
> Hi All,
>
> I am getting error while running following script.Error is due to
> improper if else loop and curly braces.
>
> Can you please guide me in placing the braces in right position and
> doing proper if..else looping
>
> My requirement is that I want to quit as soon as "if condition"
> fails.Please guide
>
>

Whitespace is your friend. If you indent blocks it makes it much easier
to figure out how the braces match up. I haven't actually looked too
much at the code, but as already mentioned "else"s should only be at the
end of conditionals. Also if you're trying to just use "else" to
quit, it's normally more concise to negate the conditional test and exit
there. Along the lines of: if ($menu_item != 1) { quit() }; (or the
block-less inverted if when you're comfortable using it...it looks as
though you should focus on the long way for now).


if ($menu_item == 1) {
$pvob=`$CT lsvob -s | grep apc`;
chomp($pvob);
my @proj=`$CT lsproj -invob $pvob | cut -d " " -f3`;
$len=@proj;
print "The projects are.....\n\n";
my $i=1;
foreach my $line(@proj) {
print "\t" . $i++ . ". $line\n";
}
print "Your Choice :\t";
chomp($choice = <STDIN> );
if ($choice < $len) {
print "Your choice: $proj[$choice-1]\n";
chomp($pro=$proj[$choice-1]);
chomp($proj1=$pro . "@" . $pvob);
#print "$proj1\n";
my $int_str=`$CT desc -fmt "%[istream]p" project:$proj1`;
} else {
quit();
} elsif ($int_str eq "") {
print "This project does not have Integration stream , so
quiting.....\n"; exit;
} else{
print "Integration Stream:$int_str\n";
dev_strm();
}
} else {
quit();
}
> Regards
> Irfan
>
>
>
>


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com