Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl nested IF loop

Status
Not open for further replies.

fatkid

Programmer
Feb 26, 2002
11
US
Hi,
When trying to run my perl script to purge old data files I get this error. Since I am not very farmilar with Perl I really have no idea what is causeing this error and I can't get the debugger to work correctly to help me out either. Any ideas or suggestions?
(Note - Numbers are put in place with the {set nu} command in vi. They are not actually part of the script.)

syntax error at temp.pl line 31, near "else"
syntax error at temp.pl line 39, near "}"

SCRIPT

18 @Files = </dir/*/*>;
19
20 if (@Files eq </dir/save/*>) {
21
22 foreach $File (@Files){
23
24 if (-d $File){}
25
26 elsif (-M $file > 60){
27 unlink $File;
28
29 }else{}
30
31 else {foreach $File (@Files) {
32
33 if (-d $File){}
34
35 elsif (-M $File > 7){
36 unlink $File;
37
38 }else{}
39 } }

If I use the script this way it works fine. Why doesn't it work with the if statement?

@Files = </dir/*/*>;
foreach $File (@Files){

if (-d $File){}

elsif (-M $File > 7){
unlink $File;

}else{}
}

Thanks
 
Just a couple of misplaced brackets, I added lines with #, moved some stuff around.:
Code:
20 if (@Files eq </dir/save/*>) {
21
22         foreach $File (@Files){
23
24                 if (-d $File){}
25
26                 elsif (-M $file > 60){
27                         unlink $File;
28
29                 }else{}
##         }
30
31 }else{
##         foreach $File (@Files){
32
33                if (-d $File){}
34
35                elsif (-M $File > 7){
36                    unlink $File;
37
38                }else{}
##         }
39 }

___________________________________
[morse]--... ...--[/morse], Eric.
 
On first glance it looks like you're missing a closing brace for the first if and first foreach statement. Try putting two closing braces on line 30.

 
Ok that seems to work. Thank you very much.
So does this mean that brackets should not be put on the same line?
IE..
they should be placed
}
}
rather than
}}

Sorry, just trying to understand my mistake so I don't make it again.
 
Its not that brackets cant be on the same line, I just do it that way to better organize the script.

___________________________________
[morse]--... ...--[/morse], Eric.
 
Ok, thank you very much.
Seems to work great with things in the right place and enough brackets.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top