new to Perl - please verify my script?

Z

zig

Hi - I'm relatively new to Perl and I'm looking to write a custom
script to be used with ClickCartPro for calculating shipping rates.
Normally this is relatively straightforward but I would like to declare
two conditions at the same time: determine which country to ship to
dependant upon the variable '$fd_trackitem_shipcountry' and then
calculate the shipping in that category based upon the total quantity.
So far I have the script below but it returns the correct country +
'Rest of the World' and the base price for 1 unit.

If anyone has the time to look and possibly verify my script, I would
be very oblidged:




######### My rather poor attempt at
######### re-arranging the order
######### of the statements.
#########
######### First declaration:
#########
#########

$custom_code_result = "";

my $ship_meth_name1 = "UK Delivery";
my $ship_total1 = "0.00";
my $ship_display1 = "";


if ($item_quantity = "1") {

$ship_total1 = "2.00";


}elsif ($item_quantity = "2") {

$ship_total1 = "2.50";


}elsif ($item_quantity = "3") {

$ship_total1 = "3.50";


}elsif ($item_quantity = "4") {

$ship_total1 = "4.00";


}elsif ($item_quantity > "4") {

$ship_total1 = "4.00";


}

if (($fd_trackitem_shipcountry eq "United
Kingdom")||($fd_trackitem_shipcountry eq "Highlands & Islands
Scotland")

||($fd_trackitem_shipcountry eq "Isles Of
Scilly")||($fd_trackitem_shipcountry eq "Isle of

Man")||($fd_trackitem_shipcountry eq "Channel
Islands")||($fd_trackitem_shipcountry eq "Northern Ireland")){



$ship_display1 = "$ship_meth_name1 - $currency_symbol$ship_total1";

$ship_total1 = sprintf("%.2f", $ship_total1);

$custom_code_result .= <<ENDOFTEXT;

<CENTER>

<TABLE WIDTH="100\%" >

<TR BGCOLOR="$html_pri_tablerow_color">

<TD VALIGN="TOP">

<INPUT TYPE="RADIO" NAME="shipinfo-$shipid"
VALUE="$ship_meth_name1\:$ship_total1" CHECKED> <FONT

FACE="$html_base_font_face" SIZE="$html_base_font_size"
COLOR="$html_base_font_color">

$ship_display1

</FONT></TD>
</TR>


</TABLE>

</CENTER>

<BR>

ENDOFTEXT

}

######### Second set of declarations:
#########
#########
#########
#########
#########
#########

my $ship_meth_name2 = "European Delivery";
my $ship_total2 = "0.00";
my $ship_display2 = "";

if ($item_quantity = "1") {

$ship_total2 = "3.00";


}elsif ($item_quantity = "2") {

$ship_total2 = "4.00";


}elsif ($item_quantity = "3") {

$ship_total2 = "5.00";

}elsif ($item_quantity = "4") {

$ship_total2 = "6.00";


}elsif ($item_quantity > "4") {

$ship_total2 = "6.00";

}


if (($fd_trackitem_shipcountry eq
"Ireland")||($fd_trackitem_shipcountry eq

"Cyprus")||($fd_trackitem_shipcountry eq
"Finland")||($fd_trackitem_shipcountry eq

"Gibraltar")||($fd_trackitem_shipcountry eq "Hungary")
||($fd_trackitem_shipcountry eq

"Iceland")||($fd_trackitem_shipcountry eq "Norway")
||($fd_trackitem_shipcountry eq

"Poland")||($fd_trackitem_shipcountry eq "San
Marino")||($fd_trackitem_shipcountry eq "Slovenia")

||($fd_trackitem_shipcountry eq
"Switzerland")||($fd_trackitem_shipcountry eq "Austria")

||($fd_trackitem_shipcountry eq "Belgium")
||($fd_trackitem_shipcountry eq "Denmark")

||($fd_trackitem_shipcountry eq "France") ||($fd_trackitem_shipcountry
eq "Germany") ||($fd_trackitem_shipcountry

eq "Greece") ||($fd_trackitem_shipcountry eq "Italy")
||($fd_trackitem_shipcountry eq "Luxembourg")

||($fd_trackitem_shipcountry eq "Netherlands")
||($fd_trackitem_shipcountry eq "Portugal")

||($fd_trackitem_shipcountry eq "Spain") ||($fd_trackitem_shipcountry
eq "Sweden")){



$ship_display2 = "$ship_meth_name2 - $currency_symbol$ship_total2";

$ship_total2 = sprintf("%.2f", $ship_total2);


$custom_code_result .= <<ENDOFTEXT;

<CENTER>

<TABLE WIDTH="100\%" CELLPADDING="10" CELLSPACING="3">

<TR BGCOLOR="$html_pri_tablerow_color">

<TD VALIGN="TOP">

<INPUT TYPE="RADIO" NAME="shipinfo-$shipid"
VALUE="$ship_meth_name2\:$ship_total2" CHECKED> <FONT

FACE="$html_base_font_face" SIZE="$html_base_font_size"
COLOR="$html_base_font_color">

$ship_display2

</FONT></TD>

</TR>

</TABLE>

</CENTER>

<BR>

ENDOFTEXT

}



######### Third set of desclarations:
#########
#########
#########
#########
#########
#########


my $ship_meth_name3 = "Rest of the World Delivery";
my $ship_total3 = "0.00";
my $ship_display3 = "";


if ($item_quantity = "1") {

$ship_total3 = "4.00";


}elsif ($item_quantity = "2") {

$ship_total3 = "6.00";


}elsif ($item_quantity = "3") {

$ship_total3 = "8.00";


}elsif ($item_quantity = "4") {

$ship_total3 = "9.00";


}elsif ($item_quantity > "4") {

$ship_total3 = "9.00";


}


$ship_display3 = "$ship_meth_name3 - $currency_symbol$ship_total3";

$ship_total3 = sprintf("%.2f", $ship_total3);


$custom_code_result .= <<ENDOFTEXT;

<CENTER>

<TABLE WIDTH="100\%" CELLPADDING="10" CELLSPACING="3">

<TR BGCOLOR="$html_pri_tablerow_color">

<TD VALIGN="TOP">

<INPUT TYPE="RADIO" NAME="shipinfo-$shipid"
VALUE="$ship_meth_name3\:$ship_total3" CHECKED> <FONT

FACE="$html_base_font_face" SIZE="$html_base_font_size"
COLOR="$html_base_font_color">

$ship_display3

</FONT></TD>

</TR>

</TABLE>

</CENTER>

<BR>

ENDOFTEXT
 
J

Julia De Silva

if ($item_quantity = "1") {

EEEEK !

Amongst lots of other points in your code >>> don't confuse = with == and
eq. Decide if you are comparing numbers or strings other wise you're going
to get into trouble.

if ($item_quantity == 1) {} # two = signs for numeric comparison (or != for
not equal)

if ($item_quantity eq "1") {} # eq for string comparison (or ne for not
equal)

J
 
A

Anno Siegel

Rob Campbell said:
zig wrote:



I'm sure there'll be other comments, but here's one. It would be neater not
to use these multiple elsif statements. You can store the shipping total
and item quantities in a hash. e.g.
my %item = (1, 3.0,
2, 4.5,
3, 5.5,
4, 6.0);

You can then reference the price using the item quantity. So to see the cost
of shipping 3 items you can just type:
print $item{3} ;

Or even

sub shipping {
my $x = shift;
(4 + 9*$x - $x**2)/4;
}

It hapens to be a quadratic function.

Anno
 
Z

zig

My thanks to you both for replying. I have amended my most basic errors
to:

if ($item_quantity == "1") {

$ship_total1 = "2.00";

throughout, but now my overwhelming problem seems to be the flow of the
script; I am attempting to determine firstly which of the 3 shipping
categories the user falls into through identifying the
'$fd_trackitem_shipcountry eq' variable, then apply the correct postage
due to the quantity within that category. I know I've made a right old
mess of this, but any general pointers as to a correct structure/flow
would be greatly appreciated.
 
A

Anno Siegel

zig said:
Hi - I'm relatively new to Perl and I'm looking to write a custom
script to be used with ClickCartPro for calculating shipping rates.
Normally this is relatively straightforward but I would like to declare
two conditions at the same time: determine which country to ship to
dependant upon the variable '$fd_trackitem_shipcountry' and then
calculate the shipping in that category based upon the total quantity.
So far I have the script below but it returns the correct country +
'Rest of the World' and the base price for 1 unit.

If anyone has the time to look and possibly verify my script, I would
be very oblidged:

There appear to be lots of little and not-so-little mistakes in your
code (a few have been pointed out). Most of all your code suffers
from doing in logic and flow-control what would be better encoded in
a data structure.

So you have a number of delivery destinations and for each a different
set of shipping costs for various quantities. This invites a hash of
lists where the hash key represents the destination and the list contains
in position $i the shipping cost for $i items. Adding a shipping cost
of 0 for 0 items presumably doesn't hurt.

my %shipping_cost = (
"UK Delivery" => [ 0, 2, 2.5, 3.5, 4],
"European Delivery" => [ 0, 3, 4, 5, 6],
"Rest of the World Delivery" => [ 0, 4, 6, 8, 9],
);


Now, if you have

my $ship_meth_name1 = 'European Delivery"
my $item_quantity = 2;

you can simply access %shipping_cost to retrieve the result without
any if's or else's:

my $ship_total1 = $shipping_cost{ $ship_meth_name1}->[ $item_quantity];

will set $ship_total1 to 4, as required.

Anno
 
J

Julia De Silva

if ($item_quantity == "1") {

$ship_total1 = "2.00";

NO NO NO I thort I'd explained that ....

if ($item_quantity eq "1") {
$ship_total1 = "2.00";

or

if ($item_quantity == 1) {
$ship_total1 = "2.00";

Oh and do you mean $ship_total1 = "2.00"; or $ship_total1 = 2.00; One is
numeric other's a string.
See the difference ?

J
 
J

Jürgen Exner

Julia De Silva wrote:
[...]
Oh and do you mean $ship_total1 = "2.00"; or $ship_total1 = 2.00; One
is numeric other's a string.
See the difference ?

He must mean a string because you never calculate prices or anything else
that is related to money using floating point numbers.
Why? Well, should have been explained when floating point numbers were
introduced in the introductory computer class or just see "perldoc -q 999"

jue
 
Z

zig

Anno, many thanks for the reply. So does this look better, defining the
country at the same time:

#######

my %shipping_cost = (
"UK Delivery" => [ 0, 2, 2.5, 3.5, 4],
"European Delivery" => [ 0, 3, 4, 5, 6],
"Rest of the World Delivery" => [ 0, 4, 6, 8, 9],
);

if (($fd_trackitem_shipcountry eq "United
Kingdom")||($fd_trackitem_shipcountry eq "Highlands & Islands
Scotland") ||($fd_trackitem_shipcountry eq "Isles Of
Scilly")||($fd_trackitem_shipcountry eq "Isle of
Man")||($fd_trackitem_shipcountry eq "Channel
Islands")||($fd_trackitem_shipcountry eq "Northern Ireland")){

my $ship_total1 = $shipping_cost{ $ship_meth_name}->[ $item_quantity];

#######
#######

$ship_display = "$ship_meth_name - $currency_symbol$ship_total";

$ship_total = sprintf("%.2f", $ship_total);

$custom_code_result .= <<ENDOFTEXT;


<CENTER>


<TABLE WIDTH="100\%" >


<TR BGCOLOR="$html_pri_tablerow_co­lor">


<TD VALIGN="TOP">


<INPUT TYPE="RADIO" NAME="shipinfo-$shipid"
VALUE="$ship_meth_name1\:$ship­_total1" CHECKED> <FONT


FACE="$html_base_font_face" SIZE="$html_base_font_size"
COLOR="$html_base_font_color">


$ship_display


</FONT></TD>
</TR>


</TABLE>


</CENTER>


<BR>


ENDOFTEXT



}


eslif if (($fd_trackitem_shipcountry eq
"Ireland")||($fd_trackitem_shi­pcountry eq

"Cyprus")||($fd_trackitem_ship­country eq
"Finland")||($fd_trackitem_shi­pcountry eq


"Gibraltar")||($fd_trackitem_s­hipcountry eq "Hungary")
||($fd_trackitem_shipcountry eq


"Iceland")||($fd_trackitem_shi­pcountry eq "Norway")
||($fd_trackitem_shipcountry eq


"Poland")||($fd_trackitem_ship­country eq "San
Marino")||($fd_trackitem_shipc­ountry eq "Slovenia")


||($fd_trackitem_shipcountry eq
"Switzerland")||($fd_trackitem­_shipcountry eq "Austria")


||($fd_trackitem_shipcountry eq "Belgium")
||($fd_trackitem_shipcountry eq "Denmark")


||($fd_trackitem_shipcountry eq "France") ||($fd_trackitem_shipcountry
eq "Germany") ||($fd_trackitem_shipcountry


eq "Greece") ||($fd_trackitem_shipcountry eq "Italy")
||($fd_trackitem_shipcountry eq "Luxembourg")


||($fd_trackitem_shipcountry eq "Netherlands")
||($fd_trackitem_shipcountry eq "Portugal")


||($fd_trackitem_shipcountry eq "Spain") ||($fd_trackitem_shipcountry
eq "Sweden")){

my $ship_total1 = $shipping_cost{ $ship_meth_name1}->[ $item_quantity];


#######
#######

$ship_display1 = "$ship_meth_name1 - $currency_symbol$ship_total1";

$ship_total1 = sprintf("%.2f", $ship_total1);

$custom_code_result .= <<ENDOFTEXT;


<CENTER>


<TABLE WIDTH="100\%" >


<TR BGCOLOR="$html_pri_tablerow_co­lor">


<TD VALIGN="TOP">


<INPUT TYPE="RADIO" NAME="shipinfo-$shipid"
VALUE="$ship_meth_name2\:$ship­_total2" CHECKED> <FONT


FACE="$html_base_font_face" SIZE="$html_base_font_size"
COLOR="$html_base_font_color">


$ship_display1


</FONT></TD>
</TR>


</TABLE>


</CENTER>


<BR>


ENDOFTEXT



}


{else}

my $ship_total2 = $shipping_cost{ $ship_meth_name2->[ $item_quantity];

#######
#######

$ship_display2 = "$ship_meth_name2 - $currency_symbol$ship_total2";

$ship_total2 = sprintf("%.2f", $ship_total2);

$custom_code_result .= <<ENDOFTEXT;


<CENTER>


<TABLE WIDTH="100\%" >


<TR BGCOLOR="$html_pri_tablerow_co­lor">


<TD VALIGN="TOP">


<INPUT TYPE="RADIO" NAME="shipinfo-$shipid"
VALUE="$ship_meth_name2\:$ship­_total2" CHECKED> <FONT


FACE="$html_base_font_face" SIZE="$html_base_font_size"
COLOR="$html_base_font_color">


$ship_display2


</FONT></TD>
</TR>


</TABLE>


</CENTER>


<BR>


ENDOFTEXT



}
 
T

Tad McClellan

Julia De Silva said:
EEEEK !

Amongst lots of other points in your code >>> don't confuse = with ==


And:

You should always enable warnings when developing Perl code!


Doing so would have pointed out this bug right away...
 
J

Jürgen Exner

zig said:
Anno, many thanks for the reply. So does this look better, defining
the country at the same time:

####### [...]

if (($fd_trackitem_shipcountry eq "United
Kingdom")||($fd_trackitem_shipcountry eq "Highlands & Islands
Scotland") ||($fd_trackitem_shipcountry eq "Isles Of
Scilly")||($fd_trackitem_shipcountry eq "Isle of
Man")||($fd_trackitem_shipcountry eq "Channel
Islands")||($fd_trackitem_shipcountry eq "Northern Ireland")){

my $ship_total1 = $shipping_cost{ $ship_meth_name}->[ $item_quantity];

Arrg, that looks ugly. Whenever you notice you are repeating a piece of code
piece over and over again that is a warning sign.
You really should use a hash to store the elements and then simply check if
your $fd_trackitem exists() in that hash.

Non-optimized approach (please see "perldoc -q contains" for the fine-tuned
version)

%UK_regions = ( #we don't care about the value, we only need the keys
'United Kingdom', 0,
"Highlands & Islands Scotland", 0,
"Isles Of Scilly", 0,
"Isle of Man", 0,
"Channel Islands", 0,
"Northern Ireland" ,0);

if (exists($UK-regions{$fd_trackitem_shipcountry}))
my $ship_total1 = $shipping_cost{ $ship_meth_name}->[ $item_quantity];

jue
 
Z

zig

My apologies Jurgen, so may this be better:



$custom_code_result = "";

my %shipping_cost = (
"UK Delivery" => [ 0, 2, 2.5, 3.5, 4],
"European Delivery" => [ 0, 3, 4, 5, 6],
"Rest of the World Delivery" => [ 0, 4, 6, 8, 9],);


%UK_regions = (
"United Kingdom", 0,
"Highlands & Islands Scotland", 0,
"Isles Of Scilly", 0,
"Isle of Man", 0,
"Channel Islands", 0,
"Northern Ireland", 0);

%European_regions = (
"Austria", 0,
"Belgium", 0,
"Cyprus", 0,
"Denmark", 0,
"France", 0,
"Germany", 0,
"Gibraltar", 0,
"Greece", 0,
"Ireland", 0,
"Finland", 0,
"Hungary", 0,
"Iceland", 0,
"Italy", 0,
"Luxembourg", 0,
"Netherlands", 0,
"Norway", 0,
"Poland", 0,
"Portugal", 0,
"San Marino", 0,
"Slovenia", 0,
"Spain", 0,
"Sweden", 0,
"Switzerland", 0);

if (exists($UK-regions{$fd_tracki­tem_shipcountry}))
my $ship_total = $shipping_cost{ $ship_meth_name}->[
$item_quantity];

$ship_display = "$ship_meth_name - $currency_symbol$ship_total";


$ship_total = sprintf("%.2f", $ship_total);


$custom_code_result .= <<ENDOFTEXT;

<CENTER>
<TABLE WIDTH="100\%" >
<TR BGCOLOR="$html_pri_tablerow_color">

<TD VALIGN="TOP">
<INPUT TYPE="RADIO" NAME="shipinfo-$shipid"
VALUE="$ship_meth_name\:$ship­­_total" CHECKED> <FONT
FACE="$html_base_font_face" SIZE="$html_base_font_size"
COLOR="$html_base_font_color">

$ship_display

</FONT></TD>
</TR>


</TABLE>


</CENTER>


<BR>


ENDOFTEXT



}


elsif (exists($European-regions{$fd_tracki­tem_shipcountry}))
my $ship_total1 = $shipping_cost{ $ship_meth_name1}->[
$item_quantity];

$ship_display1 = "$ship_meth_name1 - $currency_symbol$ship_total1";

$ship_total1 = sprintf("%.2f", $ship_total1);

$custom_code_result .= <<ENDOFTEXT;

<CENTER>
<TABLE WIDTH="100\%" >
<TR BGCOLOR="$html_pri_tablerow_color">

<



TD VALIGN="TOP">
<INPUT TYPE="RADIO" NAME="shipinfo-$shipid"
VALUE="$ship_meth_name2\:$ship­­_total2" CHECKED> <FONT
FACE="$html_base_font_face" SIZE="$html_base_font_size"
COLOR="$html_base_font_color">

$ship_display1

</FONT></TD>
</TR>
</TABLE>
</CENTER>
<BR>
ENDOFTEXT
}

}else{

my $ship_total2 = $shipping_cost{ $ship_meth_name2->[ $item_quantity];

$ship_display2 = "$ship_meth_name2 - $currency_symbol$ship_total2";
$ship_total2 = sprintf("%.2f", $ship_total2);
$custom_code_result .= <<ENDOFTEXT;
<CENTER>
<TABLE WIDTH="100\%" >

<TR BGCOLOR="$html_pri_tablerow_co­­lor">


<TD VALIGN="TOP">


<INPUT TYPE="RADIO" NAME="shipinfo-$shipid"
VALUE="$ship_meth_name2\:$ship­­_total2" CHECKED> <FONT


FACE="$html_base_font_


face" SIZE="$html_base_font_size"
COLOR="$html_base_font_color">

$ship_display2

</FONT></TD>
</TR>
</TABLE>
</CENTER>
<BR>
ENDOFTEXT
}
 
A

Anno Siegel

zig said:
Anno, many thanks for the reply. So does this look better, defining the
country at the same time:

Please, please, please quote some context with your reply. It may
be easy for you to remember what all this is about because you're
only following "your" thread. It is much harder for me and the other
regulars because we tend to be involved in a dozend threads at once.

Anno
 
Z

zig

Please, please, please quote some context with your reply. It may
be easy for you to remember what all this is about because you're
only following "your" thread. It is much harder for me and the other
regulars because we tend to be involved in a dozend threads at once.

Apologies - not used to this type of forum. Thought it was
chronological.
 
Z

zig

Anno said:
you can simply access %shipping_cost to retrieve the result without
any if's or else's:

my $ship_total1 = $shipping_cost{ $ship_meth_name1}->[ $item_quantity];

will set $ship_total1 to 4, as required.

Anno - I took some time-out on this and have managed to get the script
working thanks to everyone's input. My only remaining issue is that of
defining all orders above 4 units to be the same price shipping as that
of 4. I have tried adding the following if statement, but for some
reason it's not working. I have tried many variations [against my
syntax] but with no result. So far I have tried this:

if ($item_quantity => 4){
my $ship_total = $shipping_cost{ $ship_meth_name}->[4];
}
else{
my $ship_total = $shipping_cost{ $ship_meth_name}->[ $item_quantity];
}


If you should get a chance to advise I would very much appreciate your
input.
 
Z

zig

Jim said:
Make that

if ($item_quantity >= 4){

Jim - just tried that and it doesn't return a value for $ship_total.
Maybe it's got something to do with where I've placed it? Here's the
full code, if it helps and thanks for taking the time:


$custom_code_result = "";

%shipping_cost = (
"UK Delivery" =>
["0.00","2.00","2.50","3.50","4.00"],
"European Delivery" =>
["0.00","3.00","4.00","5.00","6.00"],
"Rest of the World Delivery" =>
["0.00","4.00","6.00","8.00","9.00"],);


%UK_regions = (
"United Kingdom", 0,
"Highlands & Islands Scotland", 0,
"Isles Of Scilly", 0,
"Isle of Man", 0,
"Channel Islands", 0,
"Northern Ireland", 0);

%European_regions = (
"Austria", 0,
"Belgium", 0,
"Cyprus", 0,
"Denmark", 0,
"France", 0,
"Germany", 0,
"Gibraltar", 0,
"Greece", 0,
"Ireland", 0,
"Finland", 0,
"Hungary", 0,
"Iceland", 0,
"Italy", 0,
"Luxembourg", 0,
"Netherlands", 0,
"Norway", 0,
"Poland", 0,
"Portugal", 0,
"San Marino", 0,
"Slovenia", 0,
"Spain", 0,
"Sweden", 0,
"Switzerland", 0);

if (exists($UK_regions{$fd_trackitem_shipcountry})){
$ship_meth_name = "UK Delivery";

}elsif (exists($European_regions{$fd_trackitem_shipcountry})){
$ship_meth_name = "European Delivery";

}else {
$ship_meth_name = "Rest of the World Delivery";
}

if ($item_quantity >= 4){
my $ship_total = $shipping_cost{ $ship_meth_name}->[4];
}
else {
my $ship_total = $shipping_cost{ $ship_meth_name}->[ $item_quantity];
}


my $ship_display = "$ship_meth_name -
$currency_symbol$ship_total<br><i>(order more than 4Ts and the rest
ship for free!)</i>";

$ship_total = sprintf("%.2f", $ship_total);

$custom_code_result = "";

$custom_code_result .= <<ENDOFTEXT;

<CENTER>
<TABLE WIDTH="100\%" >
<TR BGCOLOR="$html_pri_tablerow_color">

<TD VALIGN="TOP">
<INPUT TYPE="RADIO" NAME="shipinfo-$shipid"
VALUE="$ship_meth_name:$ship_total" CHECKED> <FONT
FACE="$html_base_font_face" SIZE="$html_base_font_size"
COLOR="$html_base_font_color">

$ship_display

</FONT></TD>
</TR>


</TABLE>


</CENTER>


<BR>


ENDOFTEXT
 
A

Anno Siegel

zig said:
Jim - just tried that and it doesn't return a value for $ship_total.
Maybe it's got something to do with where I've placed it? Here's the
full code, if it helps and thanks for taking the time:

[most code snipped]

If you post code, please don't dump your program to the newsgroup as-is,
reduce it to a minimal example that still exhibits the problem you're
facing. In your case, the error is right where you use $ship_total:
if ($item_quantity >= 4){
my $ship_total = $shipping_cost{ $ship_meth_name}->[4];

Here you declare $ship_total for the first time in the program. It is
valid inside the "if" block, nowhere else.
}
else {
my $ship_total = $shipping_cost{ $ship_meth_name}->[ $item_quantity];

You declare another (different) variable $ship_total, this time valid
in the "else" block, but not outside.

At this point the lexical variable $ship_total is non-existent, in either
case. That's the meaning of "lexical scope".
my $ship_display = "$ship_meth_name -
$currency_symbol$ship_total<br><i>(order more than 4Ts and the rest

The variable $ship_total you are using here has nothing to do with the
ones you declared above. Indeed, under "strict" Perl would have died
at this point, pinpointing your error. As it is, it has used the package
variable $ship_total, which is undefined.

Always put

use strict;
use warnings;

at the top of every program.

Anno
 
Z

zig

Jim said:
Take away the 'my' in both branches and the value will survive. (Make sure >to declare $ship_total at a higher level and inside the block where it
is needed.)
Anno Siegel wrote:
If you post code, please don't dump your program to the newsgroup as-is,
reduce it to a minimal example that still exhibits the problem you're
facing..

Gentlemen, many thanks to you both [+ all other contributors] for such
precise instructions. I have learnt a lot, not least the etiquette of
comp.lang.perl.misc. Here's the result, for anyone else interested:

my $ship_total = "0.00";

if ($item_quantity >= 4){
$ship_total = $shipping_cost{ $ship_meth_name}->[4];
}
else {
$ship_total = $shipping_cost{ $ship_meth_name}->[ $item_quantity];
}
 
T

Tad McClellan

zig said:
Here's the result, for anyone else interested:

my $ship_total = "0.00";
^^^^^^
^^^^^^

Including that is misleading, as the value set there will never
be the value after executing the statements below, it's going
to get stomped on either way.

That sure does look like a floating point number, you should consider
using integer cents instead of floating point dollars, unless you
are well-studied in matters of accuracy and precision.

if ($item_quantity >= 4){
$ship_total = $shipping_cost{ $ship_meth_name}->[4];
}
else {
$ship_total = $shipping_cost{ $ship_meth_name}->[ $item_quantity];
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top