ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> activeperl
activeperl
RE: Basic PERL Script
by Frederick Washburn other posts by this author
May 6 2008 8:51AM messages near this date
Re: Basic PERL Script | RE: Basic PERL Script
Hi Bernard,

 

You really should consider indenting your code to show proper scope.
It's not clear what you are trying to do here, but it appears you either
have a extra "}" or are missing an "} else {" after the "perform the
calculation" If the 2 lines are supposed to go together in the "if"
statement, just remove the lone "}" between them, otherwise make it an
"} else {".

 

Also you probably really want to do a modulus '%' 2 in the if statement
as what you have here will only execute one time during the loop, you
may also want to increment your loop by 2 instead of 1 since you are
processing 2 array elements per pass.

 

Your statements of the form "$output = $output . stuff" can be written
"$output .= stuff"

 

Rick

 

________________________________

From: activeperl-bounces@[...].com
[mailto:activeperl-bounces@[...].com] On Behalf Of
Bernard Hill
Sent: Tuesday, May 06, 2008 7:08 AM
To: activeperl@[...].com
Subject: Basic PERL Script

 

Good morning, All. 

Not sure if this list supports basic questions, but I'll pose the query
to find out. 

I am new to PERL and working on an basic PERL script. The script will
import values into an array. when complete, I want to do a calculation
based on two feilds and drop the answer into the third field. Basically
I have the following script, but keep getting a syntax error and can't
figure out where I've gone wrong: 

sub { 
$output = ''; 

chomp $_[0]; 
$delim = $_[1]; 
@input_fields = split /$delim/, $_[0]; 

# input the values into the array 
for($i=0; $i
<$#input_fields; $i++) { > if i$ == 2 
{ 
# perform the calculation 
$input_fields[$i] = $input_fields[$i-1] * $input_fields[$i-2]; 
} 
output = $output . $input_fields[$i] . $delim; 
} 

$output = $output . $input_fields[$#input_fields] . "\n"; 
return($output); 
} 

Any help is grealy appreciated! 

Barney 
Thread:
Bernard Hill
Andy Bach
Frederick Washburn
Brian Raven
Bill Luebkert

Privacy Policy | Email Opt-out | Feedback | Syndication
© 2004 ActiveState, a division of Sophos All rights reserved