Topic: How to access a range within a mulidimensional array Pages that link to <a href="https://ozoneasylum.com/backlink?for=31520" title="Pages that link to Topic: How to access a range within a mulidimensional array" rel="nofollow" >Topic: How to access a range within a mulidimensional array\

 
Author Thread
ruffy
Nervous Wreck (II) Inmate

From:
Insane since: Jun 2008

IP logged posted posted 12-07-2009 21:23 Edit Quote

My javascript array has values for "chapter", "paragraph",
"sentence", and the text of this sentence.
It looks like this:

book = [
[1, 1, 1, "Hi Mary."],
[1, 1, 2, "Today's Tuesday"],
[1, 1, 3, "We came back from fishing."],
...
...
[4, 26, 9, "We then took a nap."],
[4, 26, 10, "That was Tuesday"],
[4, 27, 1, "On Wednesday it was raining."],
...
...
[8, 84, 6, "Then he waved goodbye."]
];

I need to sequentially process chapters at a time.
E.g., to calculate average wordcount per paragraph in chapter 4.

How do I point into this array? What's the best way
for me to start sequential processing within this array?
The only parameters I get are the 1st and last chapter
numbers, i.e., the first element in the first and last
entries (inclusive) of the range.

E.g. How would I process from
[4, , , ] through [6, , , ]?

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

IP logged posted posted 12-08-2009 11:03 Edit Quote

Do you only want to do this to calculate word counts? Or do you want to get a new array so you can do other things with it? I'll assume the latter, since it's more generally useful.

Assuming the 'book' array is in order, you just need to write a loop that finds the first element you want, and then a loop that copies everything into a new array until you find the first element you don't want.

code:
firstChapter = 4;
lastChapter = 6;

line = 0;
// skip the lines we don't want
for ( ; line < book.length; line++ )
{
    if ( book[line][0] == firstChapter )
        break;
}

// grab the lines we do want
result = new Array();
for ( ; line < book.length; line++ )
{
    if ( book[line][0] > lastChapter )
        break; // stop when we see something we don't want
    
    result[result.length] = book[line]; // append this line to the end of the 'result' array
    // if all you want is word counts, you could just tally the word count here instead.
}

return result;




 

(Edited by Slime on 12-08-2009 11:03)

ruffy
Nervous Wreck (II) Inmate

From:
Insane since: Jun 2008

IP logged posted posted 12-08-2009 18:07 Edit Quote

Thanks Slime;

Actually I need to keep the array intact because there's
all sorts of processing that's required. But each processing
involves a range of entries, from start of an initial chapter
through the end of a chapter.

I came across the SLICE function. Might this be a way to
do it?

Although I'm still struggling with the issue, I'll look more
into your previos code and see if it can still help me out.

coach
Nervous Wreck (II) Inmate

From:
Insane since: May 2011

IP logged posted posted 05-31-2011 11:03 Edit Quote
Edit TP: spam removed


Post Reply
 
Your User Name:
Your Password:
Login Options:
 
Your Text:
Loading...
Options:


« BackwardsOnwards »

Show Forum Drop Down Menu