perl if else if loop not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl if else if loop not working
# 1  
Old 05-19-2011
Question perl if else if loop not working

I am trying to work with a text file which has following format and trying ti run if elseif loop but fails. Any help is appreciated

289B ship N-grp 123
289C ship N-grp 123
289D ship N-grp 123
2CE1 flight N-grp 123
2CE2 flight N-grp 123
2CE3 flight N-grp 123
2CE4 flight N-grp 123
2DAF flight N-grp 123
2DB0 air Y-Grp 123

error: syntax error at junk1 line 67, near "else if"
syntax error at junk1 line 72, near "else"
syntax error at junk1 line 77, near "else"

Code:
sub freecnt{
open (PEROUT, "<$grplst") || die ("can not open file!");
while ($LINEIN3 =<PEROUT>)
{
chomp $LINEIN3;
($indid1, $a, $b, $c) = split(' ',$LINEIN3);
# Check flight
if (($LINEIN3 =~ "flight") && ($LINEIN3 =~ "N-grp"))
{
print "$grpid $indid1 ==> in free pool OK to use\n";
}
# Check ship
else if (($LINEIN3 =~ 'ship') && ($LINEIN3 =~ 'N-Grp'))
{
print "$grpid $indid1 ==> in free pool OK to use\n";
}
# Check road
else if (($LINEIN3 =~ "road") && ($LINEIN3 =~ "N-Grp"))
{
print "$grpid $indid1 ==> in free pool OK to use\n";
}
Check in any group
else
{
print "$grpid $indid1 ==> NOT FOUND in Free Pool --- Check Before Using\n";
}
}
close (PEROUT);
}

# 2  
Old 05-19-2011
Hi,

It's difficult for me to inspect your source code without spaces at the beginning. It would be helpful to know which lines are the 67th and 72nd.

Regards,
Birei.
# 3  
Old 05-19-2011
birei here is the code with line nos.

Code:
           sub freecnt{
            open (PEROUT, "<$grplst") || die ("can not open file!");
            while ($LINEIN3 =<PEROUT>)
            {
             chomp $LINEIN3;
            ($indid1, $a, $b, $c) = split(' ',$LINEIN3);
           # Check flight
             if (($LINEIN3 =~ "flight") && ($LINEIN3 =~ "air"))
                {
                   print "$grpid $indid1 ==> in free pool OK to use\n";
                }
           
           # Check ship
##Line 67           else  if (($LINEIN3 =~ 'ship') && ($LINEIN3 =~ 'N/Grp'))
              {
                      print "$grpid $indid1 ==> in free pool OK to use\n";
                }
           # Check road
##Line 72           else   if (($LINEIN3 =~ "road") && ($LINEIN3 =~ "N/Grp"))
              {
                     print "$grpid $indid1 ==> in free pool OK to use\n";
                }
           
            Check in any group
##Line 77               else
                 {
                   print "$grpid $indid1 ==>  NOT FOUND in Free Pool ---  Check Before Using\n";
              }
           }
           close (PEROUT);
           }

# 4  
Old 05-19-2011
Hi,

Perl use the token 'elsif' instead 'else if'. Try changing them, like this (line 67):
Code:
elsif (($LINEIN3 =~ 'ship') && ($LINEIN3 =~ 'N/Grp'))
              {
                      print "$grpid $indid1 ==> in free pool OK to use\n";
                }

Regards,
Birei
This User Gave Thanks to birei For This Post:
# 5  
Old 05-20-2011
that worked.... thanks birei !!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For-loop not working

Hello all, I would like to unzip some files with a for-loop. Is there anyone who could tell me how I should do this - in a correct way? for file in $(ls); do echo gzip -d < $file | tar xf -; done The problem is the pipe - I believe. But how could I do it? I need it for the command... (4 Replies)
Discussion started by: API
4 Replies

2. UNIX for Dummies Questions & Answers

for loop not working

Hi, I have ten different files to extract. so I thought for loop will help me. but it's not working for me. Please advice what I am doing wrong. for INCI in 1 2 3 4 5 6 7 8 9 10 do tar -xvf Update_INCI11.10.002_lnx86_1of10.tar done second file name is... (3 Replies)
Discussion started by: samnyc
3 Replies

3. Shell Programming and Scripting

For loop not working

Hi All, For loop in ksh not working if it was given in the following method. simple script: for i in {1..4} do echo $i done Output: {1..4} Even below also not working :( Script: for (( c=1; c<=5; c++ )) do echo "Welcome $c times..." done Output: ./x.sh: 0403-057 Syntax error... (13 Replies)
Discussion started by: girish_satyam
13 Replies

4. Shell Programming and Scripting

while loop not working

hi everyone i am facing a very strange problem . please help me why my while loop is not working. below is my code #!/bin/ksh file=RCMC_ABC_999_080924_210813.DAR echo ${file} value=001 count=10 echo "abc" echo ${count} value=$((${value} + 1 )) echo ${value} while ] do echo... (3 Replies)
Discussion started by: aishsimplesweet
3 Replies

5. Shell Programming and Scripting

Perl - pass shell-vars into perl for input loop

I need to process a file line-by-line using some value from a shell variable Something like:perl -p -e 's/$shell_srch/$shell_replace/g' input.txt I can't make the '-s' work in the '-p' or '-n' input loop (or couldn't find a syntaxis.) I have searched and found... (4 Replies)
Discussion started by: alex_5161
4 Replies

6. UNIX for Advanced & Expert Users

If loop is not working

The following piece of code is not running because it is fails to go inside the if condition. i want to create a directory if there is no directory in the input path. i am using Linux, by CENT. Please help. echo " Enter the path where you u want to extract the tar" read EXTRACT_PATH ls -ld... (12 Replies)
Discussion started by: toanilsharma1
12 Replies

7. UNIX for Dummies Questions & Answers

If then else loop not working

If then else segment of below code is not working. For each filename code is displaying output for if part as well as else part. Please help its urgent. for usercusttop in `echo ${filename}|sort|uniq|cut -c 1-${actualwordcount}` do ... (2 Replies)
Discussion started by: findprakash
2 Replies

8. Shell Programming and Scripting

For loop not working...! :(

Could some one help me on this... For loop is working...! for x in $i do for y in $j do z=`echo $y | awk '{print $2}'` if then FS=`/usr/bin/echo $y` echo $FS >>$Basic_location/out.csv fi done CPRT="Cpoyright @ BTOIDCIM" done (3 Replies)
Discussion started by: bullz26
3 Replies

9. Shell Programming and Scripting

if loop is not working

Hello i am trying to remove a line using an input file , but this depends upon user interaction here is the sample #!/bin/sh echo "Please enter whether you want to remove Profile" read value1 if ;then sed /movie/d temp.txt> temp3.txt else echo "Script didnot remove profile" fi ... (3 Replies)
Discussion started by: ranga27
3 Replies

10. Shell Programming and Scripting

Loop not working

Apologize if this is doesn't come under this group. I have a small script to find out users who last logged in to check there mail. (Tru 64 4.0, Netscape mail 3.6) -----> cat $1|awk -F: '$2=="SMTP-Accept" && $5~/@maildomain/ {s=$5;u++;las=substr($1)} END {for (i in u) {print... (2 Replies)
Discussion started by: nitin
2 Replies
Login or Register to Ask a Question