Distributing values equally inside an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Degro
Forum Newbie
Posts: 8
Joined: Fri Dec 01, 2006 4:33 pm

Distributing values equally inside an array

Post by Degro »

I have an array with 240 elements.

Inside it there are either single elements having a value, either groups of 2, 3, or 4 elements with the same value. Also, there are null values. Equal to zero.

I want to distribute the single elements and the groups equally in the array so there will be the same number of "zeros" between two groups or elements. (Each group or single element holds, as its value, the unique id of a column from a mysql database. So the groups can be identified and distinguished by a variable called $positions. - each member occupies 1, 2, 3, or 4 positions in the 240 elements array.)

An example would be: [1; 1; 1; 0; 0; 0; 0; 0; 5; 5; 0; 0; 0; 0; 3; 3; 0; 0; 0; 0; 0; 0; 0; 0; 0; 16; 0; 0; 0; ... and so on, till 240.] This should be sorted. I mean distributed somehow. How exactly, I, personally, do not know. I have tried some complicated algorithms with 1 billion FOR loops and didn't work. Please help. I'm quite desperate.

In the end, all of this should be exported to a text file in a different format. But I can handle that.

If anyone could help, I would really appreciate and love him/her for the rest of my cursed life.


Dan.
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

If I understood you correctly, you just need to move zeros and not the other elements or groups.

Am I right?
Degro
Forum Newbie
Posts: 8
Joined: Fri Dec 01, 2006 4:33 pm

Post by Degro »

No, in fact i need to move both zeros and elements. If the array looks like this:

[1;1;1;1;1;1;1;1;0;0;0;] and the groups of 1 contain two elements then it should look like this:

[1;1;0;1;1;0;1;1;0;1;1].

Any idea?
Degro
Forum Newbie
Posts: 8
Joined: Fri Dec 01, 2006 4:33 pm

Post by Degro »

i did it. but there still is a bug. but ill fix it. hopefully.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

What on earth is this supposed to be for?
Also your descriptions are very confusing because you are talking about groups of things (multiple dimensions) but your examples show nothing of the sort.
Last edited by Ollie Saunders on Tue Dec 05, 2006 7:21 am, edited 1 time in total.
Degro
Forum Newbie
Posts: 8
Joined: Fri Dec 01, 2006 4:33 pm

Post by Degro »

for an mplayer .txt playlist used on a slax that takes the values from the database where customer data is held
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Could you show me some example database data and desired output? (mplayer playlist)
Degro
Forum Newbie
Posts: 8
Joined: Fri Dec 01, 2006 4:33 pm

Post by Degro »

Code: Select all

00[1:::]
01[:::2]
02[::::]
03[:3::]
04[::4:]
05[::::]
06[:5::]
07[:::14]
08[::::]
09[:1::]
10[::::]
11[2:::]
12[::3:]
13[:::4]
14[::::]
15[::5:]
16[::::]
17[14:::]
18[::1:]
19[::::]
20[:2::]
21[:::3]
22[::::]
23[4:::]
24[:::5]
25[::::]
26[:14::]
27[:::1]
28[::::]
29[::2:]
30[::::]
31[3:::]
32[:4::]
33[::::]
34[5:::]
35[::14:]
36[::::]
37[1:::]
38[:::3]
39[::::]
40[4:::]
41[:::5]
42[::::]
43[:14::]
44[:::1]
45[::::]
46[::3:]
47[:::4]
48[::::]
49[::14:]
50[::::]
51[3:::]
52[:14::]
53[:::3] 
54[:::4]
55[::::]
56[::14:]
57[::::]
58[3:::]
59[:14::]
minute[videofilename:::] where ':' is the separator. each minute, mplayer queues the video files he has to play that minute.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

The colon separate filenames? They just look like numbers unless those really are files.
And why do you have many colons with nothing in between?
Degro
Forum Newbie
Posts: 8
Joined: Fri Dec 01, 2006 4:33 pm

Post by Degro »

its quite complicated. each video file has a minimum length of 15 seconds and a maximum of 60.
so there are 4 maximum "positions" in each minute. the colon separates 4 positions
if there are empty spaces between colons it means that in that minute there is only one video file played either because there are few video files to be distributed in one hour or because the video file has 4 positions(46->60 sec) and there are no other files that could go in that minute.

I repeat, this is the desired result :)) I still didn't find it.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Right OK, beginning to make sense now.
For this data (i've changed the filenames to letters to avoid confusion):

Code: Select all

00[a:::]
01[:::b]
02[::::]
03[:c::]
You will probably want to use an array structure like this:

Code: Select all

$minutes = array(
    array(0 => 'a'), 
    array(3 => 'b'),
    array(), 
    array(1 => 'c')
);
Degro
Forum Newbie
Posts: 8
Joined: Fri Dec 01, 2006 4:33 pm

Post by Degro »

that's not really how I did it. I just used one big array with 240 elements, each representing one "position"
initially, all elements are equal to zero, and i made an algorithm that fills the array with (plays per hour) times the filename(numeric) of the video. And then i extract from there.. and export it to .txt.

But now i have a problem... there are times when this happens:

Code: Select all

00[3::4:]
01[::::]
02[:6::]
03
04
and so on...
the problem here is that if video 3 has 3 positions and video 4 lets say 2, its a total of 5 and the mplayer will stop playing the fifth part of the video and pass to the next minute. And that's bad. I have to check somewhere if the sum is larger than 4. And do something. I dont know exactly what. I guess move the file further
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

err....I'm out.

* ole stops watching topic
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

Degro, send me some files so I can understand what you mean: klinger.ofir@gmail.com
Degro
Forum Newbie
Posts: 8
Joined: Fri Dec 01, 2006 4:33 pm

Post by Degro »

thanks a lot for the support, i appreciate it, but i fixed my script. :D im happy.:D
Post Reply