Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

comparing elements from diffrent arrays. 2

Status
Not open for further replies.

dragonkeeper

Technical User
Sep 2, 2002
9
US
the following code is not working. it appears the statement that evaluates array elements is not working as i intended.
But if i hard code a value in place of one the elements the code works as intended.What am i missing? code is pasted below.

Array.prototype.unique = function()
{
var i;
var j;
var b=new Array();

main:for(i=0;i<this.length;i++)
{
for (j=0;j<b.length;j++)
{
if(this==b[j])
{
continue main;
}

}

b[b.length]=this;


}
return b;
}
 
Why are you making the SAME comparisons TWICE? That should give you too many results, which is what I am assuming since you did not state what your intentions are nor did you state the kind of results you are getting.

mmerlinn


"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Steven Raymond
 
[&nbsp;]
After looking at your code again, I may have made some wrong assumptions in the last post.

Tell us WHAT you are trying to do and SHOW us some sample inputs and outputs.

What I don't understand about your code is why you are adding this onto the end of b[]. Just EXACTLY what are you trying to accomplish?

mmerlinn


"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Steven Raymond
 
Thanks for your input mmerlinn, here is bit more detail.

My intention is to prototype a unique function for arrays that returns a new array without duplicate elements. I believe that a unique function was added to JavaScript 1.5, but I'm coding this for IE, hence I'm stuck using JavaScript 1.3.

The problem appears to be with the evaluation after the if statement, even if the elements match the code does not step into the block of code following the if statement. Code with comments pasted below.

Array.prototype.unique = function()
{
var i;
var j;
var b=new Array();

//loops through array

main:for(i=0;i<this.length;i++)
{
//loops through array b which has initial lenght of 0
for (j=0;j<b.length;j++)
{
//compares current element from array to all elements contained in array b
//if a match is found the current iteration is stopped and the code starts the next iteration of main

if(this==b[j])
{
continue main;
}

}
//if no match is found the current element is inserted into array b.

b[b.length]=this;

}
return b;
}

Here is a portion of the array being passed;

title=" language: japanese"><span>ja</span></span>
title=" language: japanese"><span>ja</span></span>
title=" language: english"><span>en</span></span>
title=" language: english"><span>en</span></span>
title=" language: korean"><span>ko</span></span>

As have the code written.
On the first iteration the this[0] is compared to b[0] which is null,therefore there is no match the code steps over the if statement and the text is written b[0] by the next block of code.

On the second iteration the this[1] is compared to b[0], these two elements should match one another, hence the code should step into the if statement where the continue should break the iteration and step back to the block of code labeled main to begin the next iteration.
The evaluation is not working correctly and every element of the array is written to array b. The odd thing is that i can hard code the value into the evaluation and it works as expected.
 
[&nbsp;]
So, if I am reading this correctly, you are trying to create a new array (b[]) containing only the unique elements of the original array (this[]). In other words you are trying to extract all unique elements from the original array and place them in a new array. Is that a correct assumption?

mmerlinn


"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Steven Raymond
 
[&nbsp;]

Assuming that the assumption I made in the last post is correct, I think something on this order should work.

[tt]
Array.prototype.unique = function()
{
var i;
var j;
var k=0
var b=new Array();

main:for(i=0; i<this.length-1; i++)
{
for (j=i++; j<this.length; j++)
{
if(this !== this[j])
{
b[k]=this;
k++
}
}
}

return b;
}
[/tt]

Not sure this will work as written as I am weak in javascript. However, a little tweeking should get you what you want.

mmerlinn


"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Steven Raymond
 
[&nbsp;]

Dang. Forgot some semicolons.

mmerlinn


"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Steven Raymond
 
[&nbsp;]
Well, it won't work as written. Got to think about this some more. I had to do the same thing in FoxPro last week and now can't get the logic correct.

mmerlinn


"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Steven Raymond
 
[&nbsp;]
Back to your original code with a slight change.

[tt]
Array.prototype.unique = function()
{
var i;
var j;
var b=new Array();

main:for(i=0; i<this.length; i++)
{
for (j=0; j<b.length; j++)
{
if(this !== b[j])
{
b[b.length] = this;
}
}
}

return b;
}
[/tt]


mmerlinn


"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Steven Raymond
 
I've have previously tried using the not equal operator as opposed to is equal operator. This resulted in nothing being written to array b. That is what led me to using the continue statement to break the iteration upon finding a match. I don't understand why the code as originally written does not work. if i hard code the value for "this" it works perfectly.
 
[&nbsp;]
I am doing some tests to see if I can find the problem rather than just looking and making changes. So far, I am still getting the same results as you are.

mmerlinn


"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Steven Raymond
 
Thanks i really appreciate the help; I added the following alert just before the if statement alert(this+" "+ b[j]). Even when the values are identical the code steps over the if code block.
 
[&nbsp;]
The problem appears to be that since b[0] is always empty that in the first iteration of j it saves this. every time. Subsequent iterations of j work properly. Am now looking for a solution.

mmerlinn


"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Steven Raymond
 
>Here is a portion of the array being passed;
The some entries are shown without explicit showing how it is entered in "the" array really!!! What if that description is not clear and is incorrect?

Suppose the array is this, then it results as intended.
[tt]
var a=new Array(
[red]'[/red]title=" language: japanese"><span>ja</span></span>[red]',[/red]
[red]'[/red]title=" language: japanese"><span>ja</span></span>[red]',[/red]
[red]'[/red]title=" language: english"><span>en</span></span>[red]',[/red]
[red]'[/red]title=" language: english"><span>en</span></span>[red]',[/red]
[red]'[/red]title=" language: korean"><span>ko</span></span>[red]'[/red]
)
[/tt]
So check what actually is the construction of "array" to start with.
 
tsuji
The array is built using a regular Expression that is parsing a page of code. So each element of the array should be represented correctly.

var titleLang = new Array;
var language = new RegExp('title=" language:.*', "gi");
while ((getTitleLang = (language .exec(searchResults))) != null)
{
titleLang .push(getTitleLang);
}
 
[&nbsp;]
Here is some code that I got to work. Not the prettiest code I ever did, though, and I am sure there is a better way.

This returns an array with unique data PLUS empty array elements where the data is duplicated. To get just the unique data you will need to delete the empty array elements.

[tt]
var a = new Array();
a[0] = "ja";
a[1] = "ja";
a[2] = "en";
a[3] = "en";
a[4] = "ko";
var b = new Array;

function Unique(a, b){
b = a;
for(i=0; i<b.length-1; i++){
if(b != ""){
for (j=i+1; j<b.length; j++){
if(b == b[j]){
b[j] = "";
}
}
}
}

return b;
}
[/tt]


mmerlinn


"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Steven Raymond
 
tsuji,
After taking another look, i went with what you had suggested and took it a step further. I didn't write the code that is being parsed, I think a bunch of monkeys got loose in a computer lab and created the code many years ago. As usual instead of a re-write my manager wants to" fix what's there", I really hate those words. But any way i prototyped a chomp function. I know i could have used ^\w in my RegExp, but i figured that the chomp function may come in handy else where.

i changed this titleLang .push(getTitleLang);
to this titleLang .push(getTitleLang.toString().chomp());

the code now works.
 
[&nbsp;]
This ugly code returns an array with only unique elements. There must be a better way of doing this.

[tt]
var a = new Array();
a[0] = "ja";
a[1] = "ja";
a[2] = "en";
a[3] = "en";
a[4] = "ko";
var b = new Array;

function Unique(a, b){
b = a;
for(i=0; i<b.length-1; i++){
if(b != ""){
for (j=i+1; j<b.length; j++){
if(b == b[j]){
b[j] = "";
}
}
}
}
j=0;
var f = new Array();
for(i=0; i<b.length; i++){
if(b != ""){
f[j] = b;
j++;
}
}

return f;
}
[/tt]


mmerlinn


"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Steven Raymond
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top