Basic PERL Script

Basic PERL Script

am 06.05.2008 16:07:56 von Bernard Hill

--===============0558865057==
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

Good morning, All.



Not sure if this list supports basic questions, but I'll pose the que=
ry 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 b=
ased 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 figu=
re out where I've gone wrong:



sub {=20

$output =3D '';=20

=09

chomp $_[0];=20

$delim =3D $_[1];=20

@input_fields =3D split /$delim/, $_[0];=20

=09

# input the values into the array

for($i=3D0; $i<$#input_fields; $i++) {=20

if i$ == 2=20

{

# perform the calculation

$input_fields[$i] =3D $input_fields[$i-1] * $input_fields[$=
i-2];

}

output =3D $output . $input_fields[$i] . $delim;=20

}=20

=09

$output =3D $output . $input_fields[$#input_fields] . "\n";=20

return($output);=20

}=09



Any help is grealy appreciated!



Barney



--===============0558865057==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============0558865057==--

Re: Basic PERL Script

am 06.05.2008 17:48:22 von Bill Luebkert

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@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: Basic PERL Script

am 06.05.2008 17:50:04 von fwashbur

This is a multi-part message in MIME format.

--===============0776205055==
Content-class: urn:content-classes:message
Content-Type: multipart/alternative;
boundary="----_=_NextPart_001_01C8AF90.DD7A4488"

This is a multi-part message in MIME format.

------_=_NextPart_001_01C8AF90.DD7A4488
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

Hi Bernard,

=20

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 {".

=20

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.

=20

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

=20

Rick

=20

________________________________

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

=20

Good morning, All.=20

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

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:=20

sub {=20
$output =3D '';=20

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

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

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

Any help is grealy appreciated!=20

Barney=20


------_=_NextPart_001_01C8AF90.DD7A4488
Content-Type: text/html;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable




charset=3Dus-ascii">









style=3D'font-size:
10.0pt;font-family:Arial;color:navy'>Hi Bernard,



style=3D'font-size:
10.0pt;font-family:Arial;color:navy'> 



style=3D'font-size:
10.0pt;font-family:Arial;color:navy'>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 =
{“.



style=3D'font-size:
10.0pt;font-family:Arial;color:navy'> 



style=3D'font-size:
10.0pt;font-family:Arial;color:navy'>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.



style=3D'font-size:
10.0pt;font-family:Arial;color:navy'> 



style=3D'font-size:
10.0pt;font-family:Arial;color:navy'>Your statements of the form =
“$output
=3D $output . stuff” can be written “$output .=3D =
stuff”



style=3D'font-size:
10.0pt;font-family:Arial;color:navy'> 



style=3D'font-size:
10.0pt;font-family:Arial;color:navy'>Rick



style=3D'font-size:
10.0pt;font-family:Arial;color:navy'> 





size=3D3
face=3D"Times New Roman">






style=3D'font-size:10.0pt;
font-family:Tahoma;font-weight:bold'>From:
size=3D2
face=3DTahoma>
activeperl-bounces@listserv.ActiveState.com
[mailto:activeperl-bounces@listserv.ActiveState.com] style=3D'font-weight:
bold'>On Behalf Of
Bernard Hill

Sent: Tuesday, May 06, =
2008 7:08
AM

To:
activeperl@listserv.ActiveState.com

Subject: Basic PERL =
Script





style=3D'font-size:
12.0pt'> 



style=3D'font-size:
12.0pt'>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 =3D '';



chomp $_[0];

$delim =3D $_[1];

@input_fields =3D split /$delim/, $_[0];



# input the values into the array

for($i=3D0; $i

<$#input_fields; $i++) { >if i$ == 2

{

# perform the calculation

$input_fields[$i] =3D $input_fields[$i-1] * $input_fields[$i-2];

}

output =3D $output . $input_fields[$i] . $delim;

}



$output =3D $output . $input_fields[$#input_fields] . "\n"; =


return($output);

}



Any help is grealy appreciated!



Barney









------_=_NextPart_001_01C8AF90.DD7A4488--

--===============0776205055==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============0776205055==--

RE: Basic PERL Script

am 06.05.2008 18:28:55 von Brian Raven

From: activeperl-bounces@listserv.ActiveState.com
[mailto:activeperl-bounces@listserv.ActiveState.com] On Behalf Of
Bernard Hill
Sent: 06 May 2008 15:08
To: activeperl@listserv.ActiveState.com
Subject: Basic PERL Script

> Good morning, All. =


Good afternoon :-) (this is an international forum).

> =

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


Shouldn't be a problem.

> =

> 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 =3D ''; =

> =

> chomp $_[0]; =

> $delim =3D $_[1]; =

> @input_fields =3D split /$delim/, $_[0]; =

> =

> # input the values into the array =

> for($i=3D0; $i<$#input_fields; $i++) { =

> if i$ == 2 =

> { =

> # perform the calculation =

> $input_fields[$i] =3D $input_fields[$i-1] * $input_fields[$i-2]; =

> } =

> output =3D $output . $input_fields[$i] . $delim; =

> } =

> =

> $output =3D $output . $input_fields[$#input_fields] . "\n"; =

> return($output); =

> } =

> =

> Any help is grealy appreciated!

You seem to defining an anonymous subroutine that is not assigned to
anything. It is hard to see how that could ever be called.

Your code would be easier to understand if you indented it
appropriately, and posted in plain text rather than html. It is also
better to cut & paste code, rather than typing it by hand, which I am
guessing you did. Also, a short, self contained script, that we can
simply copy and run, is better that posting a code extract.

If I understand your intention correctly, you seem to be going to a lot
of trouble for something that is relatively simple in Perl (note not
PERL, 'perldoc -q "perl.*Perl"'). =


For example:

------------------------------------------
use strict;
use warnings;

sub do_stuff {
my $data =3D shift;
my $delim =3D shift;

chomp $data;
my @input_fields =3D split /$delim/, $data;
$input_fields[2] =3D $input_fields[1] * $input_fields[0];
return join($delim, @input_fields);
}

my $result =3D do_stuff("2,3", ",");
print "Result: '$result'\n";
------------------------------------------

HTH

-- =

Brian Raven =


==================== =====3D=
================
Atos Euronext Market Solutions Disclaimer
==================== =====3D=
================

The information contained in this e-mail is confidential and solely for the=
intended addressee(s). Unauthorised reproduction, disclosure, modification=
, and/or distribution of this email may be unlawful.
If you have received this email in error, please notify the sender immediat=
ely and delete it from your system. The views expressed in this message do =
not necessarily reflect those of Atos Euronext Market Solutions.

Atos Euronext Market Solutions Limited - Registered in England & Wales with=
registration no. 3962327. Registered office address at 25 Bank Street Lon=
don E14 5NQ United Kingdom. =

Atos Euronext Market Solutions SAS - Registered in France with registration=
no. 425 100 294. Registered office address at 6/8 Boulevard Haussmann 750=
09 Paris France.

L'information contenue dans cet e-mail est confidentielle et uniquement des=
tinee a la (aux) personnes a laquelle (auxquelle(s)) elle est adressee. Tou=
te copie, publication ou diffusion de cet email est interdite. Si cet e-mai=
l vous parvient par erreur, nous vous prions de bien vouloir prevenir l'exp=
editeur immediatement et d'effacer le e-mail et annexes jointes de votre sy=
steme. Le contenu de ce message electronique ne represente pas necessaireme=
nt la position ou le point de vue d'Atos Euronext Market Solutions.
Atos Euronext Market Solutions Limited Soci=E9t=E9 de droit anglais, enregi=
str=E9e au Royaume Uni sous le num=E9ro 3962327, dont le si=E8ge social se =
situe 25 Bank Street E14 5NQ Londres Royaume Uni.

Atos Euronext Market Solutions SAS, soci=E9t=E9 par actions simplifi=E9e, e=
nregistr=E9 au registre dui commerce et des soci=E9t=E9s sous le num=E9ro 4=
25 100 294 RCS Paris et dont le si=E8ge social se situe 6/8 Boulevard Hauss=
mann 75009 Paris France.
==================== =====3D=
================

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

Re: Basic PERL Script

am 06.05.2008 18:33:21 von Andy_Bach

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@wiwb.uscourts.gov
Voice: (608) 261-5738 Fax: 264-5932

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

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