Gossamer Forum
Home : General : Perl Programming :

2 Servers giving different results - help!

Quote Reply
2 Servers giving different results - help!
Hi,

Can anyone help me with this? My script is doing 2 different things on 2 different servers and I'm not sure why.

Here's the setup of my two servers:

Server 1 setup: perl, version 5.005_03 built for i386-linux
Server 2 setup: perl, v5.6.0 built for i386-linux

What's happening is this... in the code below I have 3 Economy "levels" inside $RatesExcludingGST. The script is meant to loop through the three levels and figure out which shipping rate to use, selecting the appropriate one for the weight of the order ($total_weight) and allocating the total to $shipping. The order weight I'm testing with is "125", so it should be selecting the "1000" economy level.

On server 1 it's working fine. But on server 2 it's always selecting the "5000" level.

If I remove the '5000' => '9.73' option from the levels, it works fine and selects 1000 as it should.

Can anyone tell me why?????

Thanks,

Regan.


Code:

$RatesExcludingGST = {

'Urgent' => {
'levels' => {
'5000' => '10.66'
},
'max' => '5000',
'increment' => '5000',
'excess' => '8.89'
},
'Economy' => {
'levels' => {
'1000' => '3.11',
'2000' => '4.22',
'5000' => '9.73'
},
'max' => '5000',
'increment' => '5000',
'excess' => '8.00'
}
};

$LEVELS = $RatesExcludingGST->{$method}->{'levels'};

while ( ($key, $val) = each ( %$LEVELS ) ) {

if ( $total_weight <= $key ) {
$shipping = $val; # the val equals the shipment amount
last; # this is the last loop
}
}

Last edited by:

ryel01: Aug 29, 2002, 4:05 PM
Quote Reply
Re: [ryel01] 2 Servers giving different results - help! In reply to
Quick glance though I don't see anything wrong, though i didnt study it. I would guess it is a data issue.

Have you verified the values of the variables you're working with?
Quote Reply
Re: [ryel01] 2 Servers giving different results - help! In reply to
I think the problem is that you have defined your levels as a hashref, and then you loop through the hash expecting that your results are ordered the way you entered them.

The entries of the hash are read in random order (well, almost), see the following URL:
http://www.perldoc.com/...6/pod/func/each.html
There is no way for you to tell which level is returned first, it might be 1000, but it might also be 5000. So for 125, 125 will always be smaller than the first returned value, no matter what it is.

You could for example put your levels in an array of hashrefs, sort the array and then do the loop.

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] 2 Servers giving different results - help! In reply to
Hi Yogi,

Thanks for the explanation - that could be the problem.

You couldn't give a code example of the solution could you?

thanks

regan.
Quote Reply
Re: [ryel01] 2 Servers giving different results - help! In reply to
I thought you could do that yourself.... Wink

Try this (I'm sure Paul could do it with a map one-liner...), but anyway:
Code:
for (sort {$a <=> $b} keys %$LEVELS) {
$total_weight <= $_ and $shipping = $LEVEL->{$_} and last;
}

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] 2 Servers giving different results - help! In reply to
> Try this (I'm sure Paul could do it with a map one-liner...), but anyway:

Well, I took the bait and came up with this:

Code:
map { $total_weight <= $_ and $shipping = $LEVEL->{$_} and last } sort {$a <=> $b} keys %$LEVELS

Although, I'm sure this is using map in void context which is a very bad thing. I'd recommend using your for loop here.

- wil
Quote Reply
Re: [Wil] 2 Servers giving different results - help! In reply to
Can you use last inside a map block? Smile

Last edited by:

Paul: Aug 30, 2002, 6:33 AM
Quote Reply
Re: [Paul] 2 Servers giving different results - help! In reply to
I haven't a clue. I ain't tested that code. If it works, great, but as I mentioned it's better not to use a map in this case.

- wil
Quote Reply
Re: [Wil] 2 Servers giving different results - help! In reply to
I'll answer the question for you, no it won't work as last can only be used in loops and a map isn't technically a loop Smile ...so it will die a death but if it didn't it would iterate happily until the end of the hash even if it didn't need to.

Last edited by:

Paul: Aug 30, 2002, 6:52 AM
Quote Reply
Re: [yogi] 2 Servers giving different results - help! In reply to
>>
for (sort {$a <=> $b} keys %$LEVELS) {
<<

You can just use something like:

for (sort keys %$LEVELS) {

If you are lazy like me :)
Quote Reply
Re: [Paul] 2 Servers giving different results - help! In reply to
I am also lazy, but sometimes you can't be.....

Quote:

sort SUBNAME LIST
sort BLOCK LIST
sort LIST

If SUBNAME or BLOCK is omitted, sorts in standard string comparison order
We don't want that here, do we....

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [Paul] 2 Servers giving different results - help! In reply to
Quote:
last cannot be used to exit a block which returns a value such as eval {}, sub {} or do {}, and should not be used to exit a grep() or map() operation.

from perldoc.com

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] 2 Servers giving different results - help! In reply to
Yeah. I was just messing around with what I saw on the screen to see how I could write it using map. I certainly would not recommend using map in this situation however. map is definitly best used to populate an array or a hash, and is not reccomended to be used in void context.

- wil
Quote Reply
Re: [yogi] 2 Servers giving different results - help! In reply to
>>
last cannot be used to exit a block which returns a value such as eval {}, sub {} or do {}, and should not be used to exit a grep() or map() operation.

--------------------------------------------------------------------------------


from perldoc.com
<<

I guess perldoc have been stealing my fantastic explanations Laugh

Last edited by:

Paul: Aug 30, 2002, 7:01 AM
Quote Reply
Re: [Paul] 2 Servers giving different results - help! In reply to
perldoc and you differed on the sort thing..... and I know who has got it right Wink

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] 2 Servers giving different results - help! In reply to
>>
We don't want that here, do we....
<<

I guess not Blush
Quote Reply
Re: [yogi] 2 Servers giving different results - help! In reply to
>>
perldoc and you differed on the sort thing..... and I know who has got it right
<<

Ok smarty pants Laugh
Quote Reply
Re: [yogi] 2 Servers giving different results - help! In reply to
Thank you all! The solution worked perfectly.

I would have tried to work it out myself but the code is working on a live site that needed to be fixed urgently. And when it comes to code, my brain says "I don't do urgently" Crazy.

Thanks for all your help - bloody life savers!

Smile

Regan.