Calculation with "priority" hashes

  • Thread starter Bart Van der Donck
  • Start date
B

Bart Van der Donck

Hello,

The following data are prices for holiday houses. Say '20' represents
midweek 5feb-9feb, '21' is weekend 10feb-11feb, '22' is midweek
12feb-16feb, etc

Prices are in the following format:

$price_midweek{20} = 38.15;
$price_weekend{21} = 44.78;
$price_midweek{22} = 34.84;
$price_weekend{23} = 43.98;
$price_midweek{24} = 39.94;
$price_weekend{25} = 48.17;
$price_midweek{26} = 36.04;
$price_weekend{27} = 47.09;
# 28 not available
$price_weekend{29} = 44.36;
$price_midweek{30} = 40.08;
$price_weekend{31} = 41.33;

$price_fullweek{20} = 57.22;
$price_fullweek{21} = 57.22;
$price_fullweek{22} = 61.79;
$price_fullweek{23} = 61.79;
$price_fullweek{24} = 58.40;
$price_fullweek{25} = 58.40;
$price_fullweek{26} = 58.86;
$price_fullweek{27} = 58.86;
$price_fullweek{28} = 59.54;
$price_fullweek{29} = 59.54;
# 30 and 31 not available

The prices of %price_fullweek always have priority.

$price_fullweek{20} is always the same as $price_fullweek{21},
$price_fullweek{22} is always the same as $price_fullweek{23},
$price_fullweek{24} is always the same as $price_fullweek{25}, etc.

Suppose customer wants a holiday from 22 to 26, then the price would
be 61.79 (= full week price of (midweek 22 and weekend 23)) + 58.40 (=
full week price of (midweek 24 and weekend 25)) + 36.04 (price of
midweek 26), thus total 156.23.

The price for 28 can only be booked when it's in its covering week;
because $price_weekend{28} or $price_midweek{28} don't exist.

If 30 and 31 would be in the booking, then the prices of weekend
+midweek are taken (40.08+41.33) because %price_fullweek holds no
entry for 30 and 31.

It's only possible to stay in period ranges that follow each other
(like 20-21-22-23 or 25-26-27 etc).

Is it possible to calculate the total price under such circumstances ?

Thanks very much,
 
D

Dr.Ruud

Bart Van der Donck schreef:
The following data are prices for holiday houses. Say '20' represents
midweek 5feb-9feb, '21' is weekend 10feb-11feb, '22' is midweek
12feb-16feb, etc

Prices are in the following format:

$price_midweek{20} = 38.15;
$price_weekend{21} = 44.78;
$price_midweek{22} = 34.84;
$price_weekend{23} = 43.98;
$price_midweek{24} = 39.94;
$price_weekend{25} = 48.17;
$price_midweek{26} = 36.04;
$price_weekend{27} = 47.09;
# 28 not available
$price_weekend{29} = 44.36;
$price_midweek{30} = 40.08;
$price_weekend{31} = 41.33;

$price_fullweek{20} = 57.22;
$price_fullweek{21} = 57.22;
$price_fullweek{22} = 61.79;
$price_fullweek{23} = 61.79;
$price_fullweek{24} = 58.40;
$price_fullweek{25} = 58.40;
$price_fullweek{26} = 58.86;
$price_fullweek{27} = 58.86;
$price_fullweek{28} = 59.54;
$price_fullweek{29} = 59.54;
# 30 and 31 not available

The prices of %price_fullweek always have priority.

$price_fullweek{20} is always the same as $price_fullweek{21},
$price_fullweek{22} is always the same as $price_fullweek{23},
$price_fullweek{24} is always the same as $price_fullweek{25}, etc.

Suppose customer wants a holiday from 22 to 26, then the price would
be 61.79 (= full week price of (midweek 22 and weekend 23)) + 58.40 (=
full week price of (midweek 24 and weekend 25)) + 36.04 (price of
midweek 26), thus total 156.23.

The price for 28 can only be booked when it's in its covering week;
because $price_weekend{28} or $price_midweek{28} don't exist.

If 30 and 31 would be in the booking, then the prices of weekend
+midweek are taken (40.08+41.33) because %price_fullweek holds no
entry for 30 and 31.

It's only possible to stay in period ranges that follow each other
(like 20-21-22-23 or 25-26-27 etc).

Is it possible to calculate the total price under such circumstances ?

Step 1: use an AoA.

$ perl -Mstrict -MData::Dumper -wle'
my @price;
push @price, [undef, undef, undef] for 1..10;
push @price,
[38.15, 44.78, 57.22],
[34.84, 43.98, 61.79],
[39.94, 48.17, 58.40],
[36.04, 47.09, 58.86],
[undef, 44.36, 59.54],
[40.08, 41.33, undef],
;
print Dumper \@price;
'

Variant-1:

$ perl -Mstrict -MData::Dump
push my @price,
(undef) x 10,
[38.15, 44.78, 57.22],
[34.84, 43.98, 61.79],
[39.94, 48.17, 58.40],
[36.04, 47.09, 58.86],
[undef, 44.36, 59.54],
[40.08, 41.33, undef],
;
print Dumper \@price;
'

The fullweek-price could of course also go into the initial element of
each subarray.

Variant-2:

$ perl -Mstrict -MData::Dumper -wle'
push my @price,
(undef) x 10,
[57.22, [38.15, 44.78] ],
[61.79, [34.84, 43.98] ],
[58.40, [39.94, 48.17] ],
[58.86, [36.04, 47.09] ],
[59.54, [undef, 44.36] ],
[undef, [40.08, 41.33] ],
;
print Dumper \@price;
'
 

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
474,202
Messages
2,571,057
Members
47,661
Latest member
sxarexu

Latest Threads

Top