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: [EMAIL PROTECTED]
Voice: (608) 261-5738 Fax: 264-5932
"When angry, count to four; when very angry, swear."
Mark Twain
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs