Page 1 of 2
Distributing values equally inside an array
Posted: Mon Dec 04, 2006 11:40 am
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.
Posted: Mon Dec 04, 2006 12:26 pm
by ok
If I understood you correctly, you just need to move zeros and not the other elements or groups.
Am I right?
Posted: Mon Dec 04, 2006 12:35 pm
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?
Posted: Tue Dec 05, 2006 5:26 am
by Degro
i did it. but there still is a bug. but ill fix it. hopefully.
Posted: Tue Dec 05, 2006 7:19 am
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.
Posted: Tue Dec 05, 2006 7:20 am
by Degro
for an mplayer .txt playlist used on a slax that takes the values from the database where customer data is held
Posted: Tue Dec 05, 2006 7:24 am
by Ollie Saunders
Could you show me some example database data and desired output? (mplayer playlist)
Posted: Tue Dec 05, 2006 7:29 am
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.
Posted: Tue Dec 05, 2006 7:38 am
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?
Posted: Tue Dec 05, 2006 7:42 am
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.
Posted: Tue Dec 05, 2006 7:51 am
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')
);
Posted: Tue Dec 05, 2006 7:58 am
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
Posted: Tue Dec 05, 2006 8:09 am
by Ollie Saunders
err....I'm out.
* ole stops watching topic
Posted: Tue Dec 05, 2006 11:24 am
by ok
Degro, send me some files so I can understand what you mean:
klinger.ofir@gmail.com
Posted: Tue Dec 05, 2006 4:41 pm
by Degro
thanks a lot for the support, i appreciate it, but i fixed my script.

im happy.
