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 Bill Luebkert other posts by this author
May 6 2008 8:48AM messages near this date
RE: Basic PERL Script | threads package missing is_running
Bernard Hill wrote:
>  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:

What's your syntax error - I see several.

>  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!

Would help if we had some idea of what you were trying to do.

use strict;			# always run with this
use warnings;			# always run with this

my $ret = mysub ('2;3;???', ';');	# not sure what this is supposed to do
print $ret;
exit;

sub mysub {
	my ($input, $delim) = @_;

my @input_fields = split /$delim/, $input;

# input the values into the array

my $output = '';
for (my $ii = 0; $ii < @input_fields; ++$ii) {
	my $calc = 0;
	if ($ii == 2) {
		# perform the calculation
		$calc = $input_fields[$ii-1] * $input_fields[$ii-2];
		$output .= $calc . $delim;
	} else {
		$output .= $input_fields[$ii] . $delim;
	}
}
return $output . $input_fields[-1] . "\n";

}

__END__

# this should also work depending on what you actually want to do:

sub mysub {
	my ($input, $delim) = @_;
my @input_fields = split /$delim/, $input;
return $input_fields[0] . $delim . $input_fields[1] . $delim .
   $input_fields[0] * $input_fields[1] . $delim . $input_fields[2] . "\n";
}
_______________________________________________
ActivePerl mailing list
ActivePerl@[...].com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
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