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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbee: Syntax Error

Status
Not open for further replies.

woter324

Technical User
Jan 26, 2007
179
GB
Hi,

Just starting out in PERL and hit a problem with how to form some code.

I am trying to get the Unless statement to read from an array. I guess I need something like :: or || to get the two functions to work together.

The following is my current solution with the error, fith line from the bottom:

Code:
 #!usr/bin/perl
# JRun restart menu in Perl.
$platform = "Windows";
$env = "";
# $s = "";
$jrun_home="C:\\JRun4";


if ($platform = "Windows"){
 $s="\\";
 print "\nPlatform is $platform\n";
 }
else
{
 $s = "/";
}

@exclude=(".", "..", "admin", "samples", "template.zip", "cfusion");

# Get directory list

$dirtoget="$jrun_home\\servers\\";
opendir(IMD, $dirtoget) || die("Cannot open directory");
@thefiles= readdir(IMD);
closedir(IMD);

foreach $f (@thefiles){
 unless (foreach $e (@exclude){){
  print "$f\n\n";
  }
 }
}

Any assistance would be gratefully received.

Many thanks

Woter
 
Hi Keith,

Thanks for your reply. Which brackets {}, () or both?

The loop needs to bring back all the directories in the given path, excluding those given in @exclude.

Will probably build an array from the return, but one step at a time ;-)

The way I see it, the unless statement is:
Code:
unless(argument){
some code;
}
and the foreach statement is used:
Code:
 foreach $var (@array){
some code;
}

Add them together, using the foreach statement as the argument to unless, I get the code in the original question... My brain won't work! I can't see what's wrong?
Thanks

Woter
 
Wrong operator here:

Code:
if ($platform = "Windows"){

"=" is the assingment operator. This is a common beginner error. You should be using the "eq" operator to test string equality:

Code:
if ($platform eq "Windows"){

the loops at the end are also not correct:

Code:
foreach $f (@thefiles){
 unless (foreach $e (@exclude){){
  print "$f\n\n";
  }
 }
}

sticking with your beginner code, it would be written like this:

Code:
foreach $f (@thefiles){
 foreach $e (@exclude){
  unless ($e eq $f) { 
   print "$f\n\n";
  }
 }
}

all together:

Code:
 #!usr/bin/perl
# JRun restart menu in Perl.
$platform = "Windows";
$env = "";
# $s = "";
$jrun_home="C:\\JRun4";


if ($platform eq "Windows"){
 $s="\\";
 print "\nPlatform is $platform\n";
 }
else
{
 $s = "/";
}

@exclude=(".", "..", "admin", "samples", "template.zip", "cfusion");

# Get directory list

$dirtoget="$jrun_home\\servers\\";
opendir(IMD, $dirtoget) || die("Cannot open directory");
@thefiles= readdir(IMD);
closedir(IMD);

foreach $f (@thefiles){
 foreach $e (@exclude){
  unless ($e eq $f) { 
   print "$f\n\n";
  }
 }
}

if you continue with perl you will eventually understand how the code can be improved.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
You have enclosed a curly inside plain brackets.
Code:
foreach $f (@thefiles){
 unless (foreach $e (@exclude)[red]{)[/red]{
  print "$f\n\n";
  }
 }
}
Sorry - I can't get my head round the actual solution - it is Sunday after all


Keith
 
Sorry for the loooong delay in replying, but thank you for your help on this.


Woter
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top