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!

Help with the script

Status
Not open for further replies.

uguess

Technical User
Nov 5, 2004
40
CA
<data file>
coupon_name|distribution_quantity|weight|thickness
gum|235|3|10
toothpaste|500|2|9
dove|559|5|12
shampoo|733|6|6

-----------------------
What i need to accomplish.
The script will be provided with household total that all the above coupons need to be distributed. based on following formula.

For example Household=1000

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.

coupon 2 == 1/(500/100)=2


-----------------------
This is what i have tried so far.
#! /bin/perl -w


$mailout=1000;

#$data_file = "coupon.list";
#open(data_file) or die("Could not open file!");

while (<>)
{
chomp;
$record=$_;
@fields=split(/\|/,$record);

foreach $line ( @record )

{

//help me loop thru each houshold for each coupon and distribute based on the formula.
}
}

Output results
----------------
household Coupon 1 Coupon 2
1 10 (thickness) 9
2
3 9
4 10 (thickness)
.
.
.
1000




 
I've already given you a script to do this.
Why are you asking again?
Did it not work?


Trojan.
 
it is just beyond me. Can you please help me with something simple. I don't want to use something that i dont understand.

Here is what i have come up with so far myself. I am still working my way thru.

#! /bin/perl -w


$mailout=10;

#$data_file = "coupon.list";
#open(data_file) or die("Could not open file!");

while (<>)
{
chomp;
$record=$_;
@fields=split(/\|/,$record);
@coupons=$record;

foreach $line (@coupons)
{
for ($c=1;$c<$mailout;$c++)
{
$dist=$fields[1]/$mailout; (cant get the formulat to work)
print "$dist";
print "$line\n";
}
}
}

 
I am not sure I get ur question.
The way you seem to be working

for each household (1.. number given)
for each input line .. (coupon)
you seem to be calculating a value for each coupon..


The output you have given doesnt seem to use that value.
The output seems to have the coupon thickness for each household number. I dont see the formula number being used in the output.


I have written a partial program.
Not sure if this might help you or not.
I have assumed that the number got frm the formula is the household number against which you would like to list the coupon.
mailout = 1000
eg. If the formula(household/dist) = 4 for gum
= 4 for dove
= 6 for shampoo
then
in the output you would have
Household Coupon1 Coupon2
1
2
3
4 gum dove
5
6 shampoo
.
.
1000

***********************************************
Code ::


open(DATA,"coupon.list") or die "Could not open file";

my %hash=();

$mail=1000;
foreach (<DATA>)
{

next if /^\s*$/;
my ($name,$dist,$thickness,$weight) = split/\|/;

$n = int($mail/$dist);
$hash{$n}{$name} = 1;


}



for $num(1..$mail)
{
print "$num ";
if (defined $hash{$num})
{
foreach $val(keys %{$hash{$num}})
{
print "$val ";
}
}
print "\n";
}

************************************************
I havent arranged the output.
 
The reason I wrote it the way I did is because it's the most appropriate way to do it.
You need to read that control file first and populate some kind of data structure that you can use to keep counts for each type of coupon.
That's what I did.
I even named the elements of that structure for you to make it easier and populated it with all the data from the control file even though we didn't need it for this particular process.
I did that to make it easier if you wanted to extend it to add other features.
The problem is you're asking people to build a car for you but not to use wheels and engines.
If you don't understand it then you need to work at learning how it works.
You asked me to explain all of it and yet clearly you understand some perl. I didn't want to explain every single line to you cos it would be a big waste of my time.
If you want help then you could at least put a little effort in yourself and tell me exactly what bits you don't understand. To expect me to effectively write the book on the subject for you is a bit much.
I don't mind helping you but you need to help yourself as well.


Trojan.
 
Sorry - I am a bit unsure as to exactly what is required

Does this help at all?

Code:
[b]#!/usr/bin/perl[/b]

s/(\w+)\|(\d+)\|(\d+)\|(\d+)/$coupon{$1}=1; $qty{$1}=$2; $maths{$1}=int(1\/($2\/1000));$weight{$1}=$3; $thick{$1}=$4/e while <DATA>;

print $maths{"gum"};

[blue]__DATA__
gum|235|3|10
toothpaste|500|2|9
dove|559|5|12
shampoo|733|6|6[/blue]

Kind Regards
Duncan
 
ah - now i get it

how is this?

Code:
[b]#!/usr/bin/perl[/b]

$household = 1000;

s/(\w+)\|(\d+)\|(\d+)\|(\d+)/$coupon{$1}=1; $qty{$1}=$2; $maths{$1}=int(1\/($2\/$household));$weight{$1}=$3; $thick{$1}=$4/e while <DATA>;

for ($i=1; $i<=$household; $i++) {
  print "$i\t";
  while (($key, $value) = each %coupon) {
    print "$key" if $i % $maths{"$key"} == 0;
    print "\t";
  }
  print "\n";
}

[blue]__DATA__
gum|235|3|10
toothpaste|500|2|9
dove|559|5|12
shampoo|733|6|6[/blue]

Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top