473,395 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Split long list of list items

Hello All,

I am trying to split a long list of list items ( li ) into 3 groups and
append them to a div on the page. Below is the test page that I have
created. It's fairly simple but I can't seem to get my head wrapped around
it. Anyone here can offer a boost?

http://mysite.verizon.net/microweb2/3collist.html

David J.
Aug 25 '06 #1
7 2437
Or basically, how can you split an array into spearate groups by dividing
the length of the array by 3. If the array has 15 entries, how can I grab 3
chunks of 5 out of the 15 ?

David J.


"David" <ri***@dd.comwrote in message news:cUtHg.4561$0J6.3005@trnddc02...
Hello All,

I am trying to split a long list of list items ( li ) into 3 groups and
append them to a div on the page. Below is the test page that I have
created. It's fairly simple but I can't seem to get my head wrapped around
it. Anyone here can offer a boost?

http://mysite.verizon.net/microweb2/3collist.html

David J.

Aug 25 '06 #2
David wrote:
Hello All,

I am trying to split a long list of list items ( li ) into 3 groups and
append them to a div on the page. Below is the test page that I have
created. It's fairly simple but I can't seem to get my head wrapped around
it. Anyone here can offer a boost?

http://mysite.verizon.net/microweb2/3collist.html
In the line:
var container = document.getElementByID('writeMe');

You have the wrong capitalisation for getElementById()

I think what you are trying to do is divide the li elements evenly
between 3 ul elements. If so, you need to work out how many to put
into each ul, then do it. Rather than try to munge the script you have,
I'll just post the following script give you some pointers:

function quickTest()
{
var numSections = 3; // Number of sections
var numPerSection; // Number in each section (calculate)
var extras = 0; // Remainder if not equal number
// in all sections

var oUL; // New ul element
var div = document.getElementById('pContent');
var ul = div.getElementsByTagName('ul')[0];
var lis = ul.getElementsByTagName('li');
var numPerSection = lis.length/numSections | 0;

if (numPerSection) {
extras = lis.length % numSections;
} else {
numPerSection = lis.length;
}

for (var i=0; i<numSections; i++){
oUL = document.createElement('ul');

for (var j=0; j<numPerSection; j++){
oUL.appendChild(lis[0]);
}

if (extras-- 0){
oUL.appendChild(lis[0]);
}
div.appendChild(oUL);
}
div.removeChild(ul);
}
--
Rob

Aug 25 '06 #3

David wrote:
Or basically, how can you split an array into spearate groups by dividing
the length of the array by 3. If the array has 15 entries, how can I grab 3
chunks of 5 out of the 15 ?
The algorithm is in my other reply to this thread:

1. Divide the number of items by the number of sections, the
result is the number per section - truncate any decimal part

2. If the number per section is less than 1, put all items in one
section.

3. Get numberOfItems modulo numberPerSection to find out what the
'remainder' is and distribute them evenly over the sections (put an
extra one on the first 'remainder' number of sections)
--
Rob

Aug 25 '06 #4
Yes Rob, that is exactly what I was trying to achieve. Many thanks. The
getElementById() was just a typo. I do know better than that :-)

Now to soak all in what you did... thanks bundles...

David J.


"RobG" <rg***@iinet.net.auwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
David wrote:
>Hello All,

I am trying to split a long list of list items ( li ) into 3 groups and
append them to a div on the page. Below is the test page that I have
created. It's fairly simple but I can't seem to get my head wrapped
around
it. Anyone here can offer a boost?

http://mysite.verizon.net/microweb2/3collist.html

In the line:
var container = document.getElementByID('writeMe');

You have the wrong capitalisation for getElementById()

I think what you are trying to do is divide the li elements evenly
between 3 ul elements. If so, you need to work out how many to put
into each ul, then do it. Rather than try to munge the script you have,
I'll just post the following script give you some pointers:

function quickTest()
{
var numSections = 3; // Number of sections
var numPerSection; // Number in each section (calculate)
var extras = 0; // Remainder if not equal number
// in all sections

var oUL; // New ul element
var div = document.getElementById('pContent');
var ul = div.getElementsByTagName('ul')[0];
var lis = ul.getElementsByTagName('li');
var numPerSection = lis.length/numSections | 0;

if (numPerSection) {
extras = lis.length % numSections;
} else {
numPerSection = lis.length;
}

for (var i=0; i<numSections; i++){
oUL = document.createElement('ul');

for (var j=0; j<numPerSection; j++){
oUL.appendChild(lis[0]);
}

if (extras-- 0){
oUL.appendChild(lis[0]);
}
div.appendChild(oUL);
}
div.removeChild(ul);
}
--
Rob

Aug 25 '06 #5
Can I ask you what this is?

var numPerSection = lis.length/numSections | 0;

I don't understand the pipe 0 part.

David

"David" <ri***@dd.comwrote in message news:fnvHg.20743$Te.1332@trnddc07...
Yes Rob, that is exactly what I was trying to achieve. Many thanks. The
getElementById() was just a typo. I do know better than that :-)

Now to soak all in what you did... thanks bundles...

David J.


"RobG" <rg***@iinet.net.auwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
>David wrote:
>>Hello All,

I am trying to split a long list of list items ( li ) into 3 groups and
append them to a div on the page. Below is the test page that I have
created. It's fairly simple but I can't seem to get my head wrapped
around
it. Anyone here can offer a boost?

http://mysite.verizon.net/microweb2/3collist.html

In the line:
var container = document.getElementByID('writeMe');

You have the wrong capitalisation for getElementById()

I think what you are trying to do is divide the li elements evenly
between 3 ul elements. If so, you need to work out how many to put
into each ul, then do it. Rather than try to munge the script you have,
I'll just post the following script give you some pointers:

function quickTest()
{
var numSections = 3; // Number of sections
var numPerSection; // Number in each section (calculate)
var extras = 0; // Remainder if not equal number
// in all sections

var oUL; // New ul element
var div = document.getElementById('pContent');
var ul = div.getElementsByTagName('ul')[0];
var lis = ul.getElementsByTagName('li');
var numPerSection = lis.length/numSections | 0;

if (numPerSection) {
extras = lis.length % numSections;
} else {
numPerSection = lis.length;
}

for (var i=0; i<numSections; i++){
oUL = document.createElement('ul');

for (var j=0; j<numPerSection; j++){
oUL.appendChild(lis[0]);
}

if (extras-- 0){
oUL.appendChild(lis[0]);
}
div.appendChild(oUL);
}
div.removeChild(ul);
}
--
Rob


Aug 25 '06 #6
David wrote:
Can I ask you what this is?

var numPerSection = lis.length/numSections | 0;

I don't understand the pipe 0 part.
Please don't top post, reply below a trimmed quote.

'|' is a bit-wise OR operator which causes the first part of the
expression to be converted to an integer and truncated. It is the same
as:

var numPerSection = Math.floor( lis.length / numSections );
but shorter - some also say faster, but that is irrelevant here since
you have to do about 10,000 loops to notice.
--
Rob

Aug 25 '06 #7
'|' is a bit-wise OR operator which causes the first part of the
expression to be converted to an integer and truncated. It is the same
as:
var numPerSection = Math.floor( lis.length / numSections );

but shorter - some also say faster, but that is irrelevant here since
you have to do about 10,000 loops to notice.
Sorry about the top post and trim .. noted.

Thanks for your help and explanation(s)

David J.
Aug 25 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Luka Milkovic | last post by:
Hello, I have a little problem and although it's little it's extremely difficult for me to describe it, but I'll try. I have written a program which extracts certain portions of my received...
5
by: oliver | last post by:
hi there i'm experimanting with imaplib and came across stringts like (\HasNoChildren) "." "INBOX.Sent Items" in which the quotes are part of the string. now i try to convert this into a...
3
by: David Pratt | last post by:
Hi. I am splitting a string on a non whitespace character. One or more whitespace characters can be returned as items in the list. I do not want the items in the list that are only whitespace (can...
3
by: Sambo | last post by:
I have couple of puzzles in my code. def load_headers( group_info ): if os.path.isfile( group_info.pointer_file ): ptr_file = open( group_info.pointer_file, "r" ) else: print...
1
by: maxtoroq | last post by:
I know how to work the .FindAll method of the List class, but is there a way to split a list into 2 lists, one containing all the items that match a certain criteria and one that doesn't?
24
by: garyusenet | last post by:
I'm working on a data file and can't find any common delimmiters in the file to indicate the end of one row of data and the start of the next. Rows are not on individual lines but run accross...
1
by: clayalphonso | last post by:
Here is the code: <% dim testArray, testArray2 dim Conn, rs dim sSQL, sConnString 'response.write request.form("sel1") 'testArray = split(request.form("sel1"),",") 'for each gidstuff In...
1
by: Roy | last post by:
I have a table with two fields. I wish to separate all the data in one field and keep it listed against the information in the other field. The information within the field TicketStatus, I wish to...
2
by: Shawn Minisall | last post by:
I'm trying to unpack a list of 5 floats from a list read from a file and python is telling me 5 variables are too many for the string.split statement. Anyone have any other idea's? NOTE: the only...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.