Click to See Complete Forum and Search --> : Forumla Problem
Nancy
11-18-2003, 05:35 PM
Hope that you will beable to help me in my little problem. Im sure the answer is a very simple one but for the life of me I can not figure it out.
Example:
We have 17 known items, each with a value assigned to them as shown below:
none = 0
bread = 1
cake = 2
pie = 4
apple = 8
orange = 16
grape = 32
cookie = 64
icecream = 128
pop = 256
tea = 512
coffee = 1024
candy = 2048
wine = 4096
beer = 8192
water = 16384
all = 32767
You see that all the values are double the last value.
If the total value (sum) was 146 the items would be:
icecream (128)
orange (16)
pie (4)
Instead of writing out 32767 IF lines (all the possible combinations) is there a way to make a forumla to cover all the possiblities?
I know the starting items and vales (as shown above) and the total sum values from the database but as the database is updated with possible new combinations I dont want to have to keep adding IF lines to the program.
(I hope you understand where Im going with this)
The end goal will be to get the data $item from the database and then for it to beable to list the combinations.
Thank you for your help in advance.
davey
11-29-2003, 09:48 PM
i believe something like this could be done in javascript but im not sure what to do about the database part
AdamBrill
11-29-2003, 11:31 PM
First off, your way of doing this is not the best way available... ;)
But here is how you would do it(since that is what you asked, that is what I'll give you). As you can see, this is in JavaScript, because I don't know Perl. :) However, after seeing the logic, you, or someone else on this board, should be able to port it over to Perl without too much difficulty. So here is the JavaScript function:<script type="text/javascript">
function get_values(num){
numbers = new Array('0','1','2','4','8','16','32','64','128','256','512','1024','2048','4096','8192','16384','3276 7');
var y = numbers.length;
str = "The following items were selected:\n";
while(num != 0){
for(var x=y; x>0; x--){
if(numbers[x]<=num){
str+= numbers[x]+"\n";
num -= numbers[x];
y=x-1;
break;
}
}
}
return(str);
}
alert(get_values(4164));
</script>
Change the 4164 in this line:
alert(get_values(4164));
to whatever the total of all of the items selected was. It will then return the numbers for each of the items selected. I hope that helps you, and good luck on your porting it over to Perl. ;)
If you can't get it ported over to Perl, post back and I'll see what I can do for you. Although I don't know Perl, I've got a book and enough knowledge of programming that I should be able to get it working. ;)
Nancy
11-30-2003, 07:01 AM
Thanks for your reply, however Im not great at all with javascript plus now I'm having a major problem finding a database search script that allows me to search mutli fields at once.
This whole idea if being a real pain in the tookuss :(
The only search scripts I can find is for keywords only.
If anyone knows any search programs that can search multi fields pls let me know.
You stated the way I was doing it wasn't the best way available, what would be the best way?
Thanks for your help.
AdamBrill
11-30-2003, 02:14 PM
Rather than adding them all together(which, BTW, wouldn't work if they could have a couple of the same item), I would assign each of the items to a letter. So they would look something like this:
bread = A
cake = B
pie = C
apple = D
orange = E
grape = F
cookie = G
icecream = H
pop = I
tea = J
coffee = K
candy = L
wine = M
beer = N
water = O
Then you can know which ones they selected by the string. So if they selected bread, icecream, and water, the string would look like this: AHO Then you can know which ones they selected. However, it probably should be remade totally... Are you trying to make something like a shopping cart here? If so, you might want to try getting a free shopping cart from somewhere... :) It would probably be a lot easier.
You could also use something like this. I'm sure this isn't the best Perl, but hey, I really don't know Perl - this is just what I hacked out. :p
#!/user/bin/perl
$num = 4164; #value to find numbers for
@val = ('0','1','2','4','8','16','32','64','128','256','512','1024','2048','4096','8192','16384','32767');
@vals = ();
for ($j=15; $j>0; $j--) {
for ($i=15; $i>0; $i--) {
if (@val[$i] <= $num) {
push(@vals, @val[$i]);
$num -= @val[$i]
}
}
}
#uncomment below, for use on the web
#print "Content-type: text/html\n\n";
foreach $item (@vals) {
print $item."\n";
}