Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read and assign based on formula

Status
Not open for further replies.

uguess

Technical User
Nov 5, 2004
40
CA
I have a file like below

Total to be mailed = 1000

Coupon_name Dist_quantity Thickness Wight (grams)
gum 235 3 10
toothpaste 500 5 12
shampoo 100 7 15
Dove 800 6 8

How can i assign above input to an output file based on followign forumla.

n=rounddown[1/(dist_quantity/total mailing)]

example for 1st coupon==> 235/1000 = 0.235
1/(235/1000) = 4.255
Round down to lowest number = 4
conclusion 1 in 4 people will get 1st coupon.

if 1 person out every 4 mailout will recieve coupon then output the thickness to output file every 4th maiout line.

Output coupon1
mailout 1 (output thickness here)
mailout 2
mailout 3
mailout 4
mailout 5
mailout 6 coupon 1 (thickness)
mailout 7
mailout 8
mailout 9
mailout 10


Ideas on how to approcah writting this script will be greatly appreciated. I want to do in in perl. how will i pass the formuala to the scipt and output. etc.

 
Hi there.
1) Your calc is as simple as "int(1000/235)".
2) By rounding down to 4 you will have some mailings at the end that have no coupons at all. You would be better to use the complete figure of 4.255
3) Your output shows on in 5 not one in 4. Which is it?
4) Do you want all coupons in the one mailing or a separate mailing for each coupon type?
Other than that this should be very simple.


Trojan.
 
Thanks for the reply

I would like to round down to lower rounded number. It is expected that at some point coupon will run out.

Sorry my misatake output should be 1 in 4.

All coupons should be done in one mailing. so output files should reflect all coupon assigned based on its distribution calculation.

Can you please help get me started. Thanks
 
Check this out and see if it's close to what you want:
Code:
#!/usr/bin/perl -w
use strict;

my $total   = 0;
my %coupons = ();
while(<DATA>) {
  $total = $1 if(/Total to be mailed = (\d+)\s*$/);
  if(/^(\w+)\s+(\d+)\s+(\d+)\s+(\d+)/) {
    $coupons{$1}{'quantity'}  = $2;
    $coupons{$1}{'thickness'} = $3;
    $coupons{$1}{'weight'}    = $4;
    $coupons{$1}{'modulus'}   = int($total / $2);
  }
}
for(my $c=1; $c<=$total; $c++) {
  print "mailout $c ";
  my $thickness = 0;
  foreach my $coupon (keys %coupons) {
    if($c % $coupons{$coupon}{'modulus'} == 1) {
      next unless(--$coupons{$coupon}{'quantity'});
      $thickness += $coupons{$coupon}{'thickness'};
      print "($coupon ", $coupons{$coupon}{'thickness'}, ") ";
    }
  }
  print "\n";
}

__DATA__
Total to be mailed = 1000

Coupon_name   Dist_quantity   Thickness    Wight (grams)
gum            235              3            10
toothpaste     500              5            12
shampoo        100              7            15
Dove           800              6            8



Trojan.
 
Or better still, this:
Code:
#!/usr/bin/perl -w
use strict;

my $total   = 0;
my %coupons = ();
while(<DATA>) {
  $total = $1 if(/Total to be mailed = (\d+)\s*$/);
  if(/^(\w+)\s+(\d+)\s+(\d+)\s+(\d+)/) {
    $coupons{$1}{'quantity'}  = $2;
    $coupons{$1}{'thickness'} = $3;
    $coupons{$1}{'weight'}    = $4;
    $coupons{$1}{'modulus'}   = int($total / $2);
  }
}
for(my $c=1; $c<=$total; $c++) {
  print "mailout $c ";
  my $thickness = 0;
  foreach my $coupon (keys %coupons) {
    if((($c-1) % ($coupons{$coupon}{'modulus'})) == 0) {
      $coupons{$coupon}{'quantity'}--;
      next unless($coupons{$coupon}{'quantity'} > 0);
      $thickness += $coupons{$coupon}{'thickness'};
      print "($coupon ", $coupons{$coupon}{'thickness'}, ") ";
    }
  }
  print "\n";
}

__DATA__
Total to be mailed = 1000

Coupon_name   Dist_quantity   Thickness    Wight (grams)
gum            235              3            10
toothpaste     500              5            12
shampoo        100              7            15
Dove           800              6            8


Trojan.
 
Thanks for the response

Can you please make this script simple. Also bit of explanation line by line would be great. I am newbie is there a simple way to write this.

thanks
 
Does it do what you want?
Which bits do you understand and which bits do you not?
This is quite simple but any bits you don't understand I could look at rewriting or explaining.
I don't really want to have to explain every single line cos I guess most of that would be pointless.


Trojan.
 
#!/usr/bin/perl -w
use strict;

my $total = 0;
my %coupons = (); // explain
while(<DATA>) {
$total = $1 if(/Total to be mailed = (\d+)\s*$/); // explain
if(/^(\w+)\s+(\d+)\s+(\d+)\s+(\d+)/) { // explain
$coupons{$1}{'quantity'} = $2;
$coupons{$1}{'thickness'} = $3;
$coupons{$1}{'weight'} = $4;
$coupons{$1}{'modulus'} = int($total / $2);
}
}
for(my $c=1; $c<=$total; $c++) {
print "mailout $c ";
my $thickness = 0;
foreach my $coupon (keys %coupons) {
if((($c-1) % ($coupons{$coupon}{'modulus'})) == 0) { // explain
$coupons{$coupon}{'quantity'}--; // explain
next unless($coupons{$coupon}{'quantity'} > 0);
$thickness += $coupons{$coupon}{'thickness'};
print "($coupon ", $coupons{$coupon}{'thickness'}, ") ";
}
}
print "\n";
}


can this above script be re written in simple for loop structure.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top