RE: [PHP] Array newbie

From: Date: Fri, 04 Aug 2000 19:39:13 +0000
Subject: RE: [PHP] Array newbie
Groups: php.general 
Request: Send a blank email to php-general+get-10183@lists.php.net to get a copy of this message
Well hashing is one way to avoid complex looping (loop inside a loop and possible inside another loop). I'll give you a small example. But before I do, I think that you will, either way, have to do some looping, the key is to minimize the looping complexity as much as possible. Consider This small HASH example. I'm not sure where you're from, I'm assuming US, whatever country it is, I'm sure you have taxes. Lets assume that the Tax Man wants to keep track of delinquent citizens in a table and they want to be able to quickly locate information without searching every record. Every citizen has a social security (9 digit) number that uniqely identifies them. However, being the taxman they're cheap and they don't want to allocate space for all their citizens, they're just gona allocate space for 10 delinquent accounts. SO they have an array called "Tax" of size ten. And when a delinquent account comes up they will look at the last digit of their social security account and place it into the proper array location. What do I mean? Well look at the following example. We have three people with their numbers as follows 991-110-123, 991-110-125, 991-110-128. We will look at the last number (3, 5 and 8) and store them in their corresponding array locations. Tax[3]="991-110-123; Ows $10; 123 John St."; Tax[5]="991-110-125; Ows $100; 3 Dave St."; Tax[8]="991-110-128; Ows $1000; 13 Peter St."; So if they ever wanted to retrieve information about a particular user they just have to print the corresponding array location and don't have to do any searching. HASH tables are usefull in cases when you are almost never going to have more values then you have space in your table and want to access the information without any searching. How does this help you. Well you said you will have an array of references. Name (1,3,11,13 ... 99). Well depending on exactly what you need, you could do something along the lines of: $Score_for_this_reference ["1"]+=1; //Increse the score by one $Score_for_this_reference ["3"]+=1; $Score_for_this_reference ["11"]+=1; ... $Score_for_this_reference ["99"]+=1; In the end you only have to look through the array and see how many time a particualr reference was matched. Now i'm sure I didn't answer your question exactly. I just hope you got the general idea of HASH tables that may help you in choosing the most efficient direction. Also note that the array indexes are strings. I did this on purpose becuase I think if you make them number you will creat an array of that size. In perl, and i'm guessing in php also (SOMEONE PLEASE VERIFY) if you say $Score_for_this_reference [100]+=1; (Without the quotes) it will create an array of size 100 whereas $Score_for_this_reference ["100"]+=1; will only creat an array of size 1. Mike -----Original Message----- From: David VanHorn [mailto:dvanhorn@cedar.net] Sent: Friday, August 04, 2000 3:05 PM To: Krznaric Michael Subject: RE: [PHP] Array newbie At 02:50 PM 8/4/00 -0400, you wrote: >Ahhh, I see now. So now we're back at the original question. I'm gona >phrase my assumptio again so you know what I think this is about. I went to >your webiste and read your intro, I've also reread our thread. I think your >origninal approach is good. Check the name, address and CSZ individually >and then increase the score as appropriate. You originally wanted to use >three arrays and then check if they had common keys. You were worried that >your arrays would get too big? Do I understand your problem properly? Yes.. That, and finding no easy way to determine what elements of array 1 are common to array 2 and 3 (or other combinations) without loops. I did think of an alternative approach, but maybe it's too complicated, writing new MySQL tables (with a process flag) that would contain the subject elements. This way, I could pass the flag out from the function, and then look at the results fetched.. Of course now I have to worry about destroying (ONLY) the proper tables on exit.. :-P > If this is what your problem is, the only thing I can suggest is to >consider a HASH table? It seems that from the nature of your problem, you >have to do some heavy computation either way. The only thing you can try is >to make the best of it. What approach are you considering? I'll have a look at hash tables. That's a new one on me too. :) It's nothing, if not a learning experience! -- Where's dave? http://www.findu.com/cgi-bin/find.cgi?kc6ete-9

« previous php.general (#10183) next »