1 msgSpreadsheet::ParseExcel::SaveParser
4 msgFinding a printer; need help

Basic PERL Script
\ Bernard Hill (6 May 2008)
. \ Bill Luebkert (6 May 2008)
. \ Washburn, Frederick (6 May 2008)
. \ Brian Raven (6 May 2008)
. \ Andy_Bach (6 May 2008)

14 msgthreads package missing is_running
1 msgDB_File Module not working
2 msgPERL Telnet Script problem
5 msgGetting MIME::Lite working on Windows
7 msgActivePerl 822 and Windows GUI look/feel
1 msgGD and libgd, please help
2 msgbroken perlapp in version 5-10.
1 msgperl installation wizard
5 msggetopt long
1 msgANNOUNCE: ActiveState Perl Dev Kit 7.2 released
3 msgFW: ActivePerl-5.8.8.822-MSWin32-x86-280952.msi...
2 msgRe: PerlScript - FileSystemObject - Windows Scr...
3 msgppm
7 msgActivePerl 5.10 and Oracle/ODBC
Subject:Re: Basic PERL Script
Group:Activeperl
From:Andy_Bach
Date:6 May 2008


 
Just some standard pointer first - dont' say "PERL". It's not an acronymn.
Standardly capitalized "Perl" when speaking of the language in general,
and lc "perl" when talking about specific code or script. But never PERL.
Thanks. You can google the backstory.

Next - it is important to use warning/strict in nearly anything a one
liner, but esp. if you're going to post it to Perl list. You'll (or you
*should*) hear it every time you post code - it's the easiest, cheapest
way to avoid foolish mistakes. Any stylistic etc comments below are from
my understanding of the coding bible "Perl Best Practices" (D. Conway -
O'Reilly).

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);
}

sub params are better
my ($data_str, $delim) = @_;
chomp($data_str);

you need parens around the "if" criteria
if ( $i == 2 ) {

For loops for arrays are better done perlishly
# input the values into the array
for($i=0; $i<$#input_fields; $i++) {

would normally be:
# input the values into the array
for my $fld ( @input_fields ) {

But you appear to be talking a string of numbers and delims:
2,4,6

splitting it up, multipling the first 2 and saving it in the 3rd (so your
output is 2x4:
2,4,8,

) W/ the "== 2" there, the calc. is only going to happen once, regardles
of string length, which is probably not what you wanted. If you want every
3rd field "($i + 1) % 3" maybe - +1 as arrays are zero based, and mod will
return zero so:
if ( not ($i + 1) % 3 ) {

There are many ways to do this beside your route, but it mostly should
work.

a

-------------------
Andy Bach
Systems Mangler
Internet: andy_bach
Voice: (608) 261-5738 Fax: 264-5932

"When angry, count to four; when very angry, swear."
Mark Twain

_______________________________________________
ActivePerl mailing list
ActivePerl
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


© 2004-2008 readlist.com