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 file, for loop, calculate, output

Status
Not open for further replies.

uguess

Technical User
Nov 5, 2004
40
CA
Since i am a newbie please suggest simple solution so that i can understand.

Thanks
--------------------------------------------------------

I have a file like below
<dat>
coupon dist_quantity wight thickness
gum 235 3 10
toothpaste 500 5 12
shampoo 100 7 15

What i want to get done
-----------------------

Within my script i would provide a number

$household=1000;


Based on that above household number, i want to ouput that many lines in output file. I want to calculate coupon distribution within output file based on following formula.


n=rounddown[1/(dist_quantity/houshold)]

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

500/1000=0.5 =1/0.5= 2
1 in every 2 houshold will get coupon #2


What the output will look like
household coupon#1 coupon#2 coupon#3
1 gum toothpaste
2
3 toothpaste
4 gum
5 toothpaste
6
7 toothpaste
8 gum
.
.
.
1000

In the end there is also another summary file produce that is like this:

50 household got 2 coupons
1000 houshold got 1 coupon


 
You were provided very nice/detailed information in thread219-1169075.
 
That solution was too difficult for me to grasp. I was looking for something simple. I started bit of work but lost.

>cat coupon.list
gum|235|3|10
toothpaste|500|2|9
dove|559|5|12
shampoo|733|6|6

#! /bin/perl -w

open(out1,">coupon_results.dat1");
open(out2,">coupon_results.dat2");

$mailout=1000;

$data_file = "coupon.list";
open(data_file) or die("Could not open file!");
chomp;
($couponName, $distQuantity, $Thickness, $weight) = split(/\|/,$_);
{printf out2 "$couponName\n";}

for ($c=1;$c<$mailout;$c++)
{

{printf out1 "$couponName\n";}
}

close(datafile);

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top