Forum Moderators: open

Message Too Old, No Replies

searching for specific values in an array

         

jeffturl

9:14 pm on Jan 31, 2007 (gmt 0)

10+ Year Member



Following on from an earlier post, i have an array with multiple elements and wish to look at each element to see after the # if next characters are ABC then return a true result of 1 if not then return a result of 0. here is my code so far can anyone point me in the right direction to achieve what i want?

<html>
<head>search for data in an array

<title>test </title>

<script language="JavaScript" >

var test1 = ['12345#ABC/1','123ACDE#BCC/2'];
var test2 = '';

test2 = test1[0];
for (counter = 0; counter <= test1.length; counter = counter + 1)
{

};
<BR>

document.write('The number in the array is ' + test2 + 'and so is true')

</script>

</head>

<body>
</body>

</html>

Fotiman

9:43 pm on Jan 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Did you look at my original reply?

var x = "abcd-efghi";
var dashLocation = x.indexOf("-"); // 4
var threeChars = x.substr( dashLocation + 1, 3 );

Thus, applying this to your code (but removing the NUMEROUS errors in your code first)...


<html>
<head>search for data in an array</head>
<title>test</title>
<script type="text/javascript">
var test1 = ['12345#ABC/1','123ACDE#BCC/2'];
for (var counter = 0; counter <= test1.length; counter++ )
{
var dashLocation = test1[counter].indexOf("#");
var threeChars = test1[counter].substr( dashLocation + 1, 3 );
if( threeChars == "ABC" )
{
alert("Yup... found ABC");
// Stop looping
break;
}
}
</script>
</head>
<body>
</body>
</html>

jeffturl

8:03 pm on Feb 1, 2007 (gmt 0)

10+ Year Member



Thanks very much for that, it really helped. is there a variation that i can search for A and C, so the middle charcater could be anything?

Fotiman

8:44 pm on Feb 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Maybe something like this (replace the corresponding lines from the original post):


var threeChars = test1[counter].substr( dashLocation + 1, 3 );
var reg = /[Aa][.][Cc]/;
if( reg.test(threeChars) )
{
alert("Yup... found A*C");
// Stop looping
break;
}

I *think* this will match any string that begins with an 'A' or 'a', followed by any character, followed by a 'C' or 'c'.

jeffturl

9:00 pm on Feb 1, 2007 (gmt 0)

10+ Year Member



Thanks for the advise, i cant seem to get that new bit of code working, am i just being really dense? Can someone recommend a good editor as i am just starting out i a recommendation would be helpful :

<html>
<head>search for data in an array</head>
<title>test</title>
<script type="text/javascript">
var test1 = ['12345#ABC/1','123ACDE#BCC/2'];
for (var counter = 0; counter <= test1.length; counter++ )
{
var dashLocation = test1[counter].indexOf("#");
var threeChars = test1[counter].substr( dashLocation + 1, 3 );
if( threeChars == "ABC" )
{
alert("Yup... found ABC");
// Stop looping
break;
}
}
</script>
</head>
<body>
</body>
</html>

Fotiman

4:06 pm on Feb 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Sorry, remove the [] around the . in the regExp, and fix the condition check in the for loop to be '<' instead of '<=':


<html>
<head>search for data in an array</head>
<title>test</title>
<script type="text/javascript">
var test1 = ['12345#ABC/1','123ACDE#BCC/2'];
for (var counter = 0; counter < test1.length; counter++ )
{
var dashLocation = test1[counter].indexOf("#");
var threeChars = test1[counter].substr( dashLocation + 1, 3 );
var reg = /[Aa].[Cc]/;
if( reg.test(threeChars) )
{
alert("Yup... found A*C");
// Stop looping
break;
}
}
</script>
</head>
<body>
</body>
</html>

jeffturl

7:58 pm on Feb 5, 2007 (gmt 0)

10+ Year Member



Wow, thanks for that. How long have you been programming in Javascript? I feel i have a long way to go still!

If i wanted to change the code so it does all it did before but also if the array contains the letter W to return a different result again?

Fotiman

8:54 pm on Feb 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




Wow, thanks for that. How long have you been programming in Javascript? I feel i have a long way to go still!

I've been using JavaScript for about 10 years, but I didn't REALLY start using it until the past year or so.


If i wanted to change the code so it does all it did before but also if the array contains the letter W to return a different result again?

Can you be more specific? Can you give an example of what the array might look like (what values would be in it)? Would you want it to NOT check for A.C if it found the W?

jeffturl

10:22 pm on Feb 5, 2007 (gmt 0)

10+ Year Member



sorry, what i am after is if it finds a W in an element withing the array the return return - found W, if it does not find a W then continue the search for A*C in the element in the array. So i guess the W otherrides the A*C rule if that makes sense?

Fotiman

10:54 pm on Feb 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ok, then maybe something like this:

<html>
<head>search for data in an array</head>
<title>test</title>
<script type="text/javascript">
var test1 = ['12345#ABC/1','123ACDE#BCC/2'];
var foundW = false;
var foundAxC = false;
for (var counter = 0; counter < test1.length; counter++ )
{
if( test1[counter].indexOf("W") >= 0 )
{
foundW = true;
break;
}
var dashLocation = test1[counter].indexOf("#");
var threeChars = test1[counter].substr( dashLocation + 1, 3 );
var reg = /[Aa].[Cc]/;
if( reg.test(threeChars) )
{
foundAxC = true;
// Stop looping
continue;
}
if( foundW )
{
alert("Found W");
}
else if( foundAxC )
{
alert("Found AxC");
}
else
{
alert("Didn't find W or AxC");
}
}
</script>
</head>
<body>
</body>
</html>

Note, if it finds a W, it will break out of the for loop. But if it finds an AxC, it will continue to the next item in the loop. It does that in case it finds an AxC before finding a W that appears later in the array.

[edited by: Fotiman at 10:54 pm (utc) on Feb. 5, 2007]

jeffturl

7:58 pm on Feb 6, 2007 (gmt 0)

10+ Year Member



Thanks for the help, but it does not appear to find W when i put it anywhere in the array. in upper or lower case.

I see it is declared and initialised with a value of false, and there is an if statement but whereabouts is the code that is looking for W? i can follow the logic for the ABC statement, but not quite following the search and stop for the W.

jeffturl

8:14 pm on Feb 6, 2007 (gmt 0)

10+ Year Member



I think i understand what it is doing now! as opposed to breaking the loop, can i get it to produce an output such as as a element1 is (display element1) contains W
element2 is (display element2) contains ABC

even if element1 contains ABC the W again will take priority over it?

Fotiman

9:14 pm on Feb 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Just realized that all of my examples seem to be horribly screwed up (<head>search for data in an array</head> ?).

Fotiman

9:20 pm on Feb 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Here's what that last post should have looked like:

<html>
<head>
<title>test</title>
<script type="text/javascript">
var test1 = ['12345#ABC/1','123ACDE#BCC/W'];
var foundW = false;
var foundAxC = false;
for (var counter = 0; counter < test1.length; counter++ )
{
if( test1[counter].indexOf("W") >= 0 )
{
foundW = true;
break;
}
var dashLocation = test1[counter].indexOf("#");
var threeChars = test1[counter].substr( dashLocation + 1, 3 );
var reg = /[Aa].[Cc]/;
if( reg.test(threeChars) )
{
foundAxC = true;
// Stop looping
continue;
}
}
if( foundW )
{
alert("Found W");
}
else if( foundAxC )
{
alert("Found AxC");
}
else
{
alert("Didn't find W or AxC");
}
</script>
</head>
<body>
</body>
</html>

[edited by: Fotiman at 9:20 pm (utc) on Feb. 6, 2007]

Fotiman

9:29 pm on Feb 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Try this one:


<html>
<head>
<title>test</title>
<script type="text/javascript">
var test1 = ['12345#ABC/1','123ACDE#BCC/2','123ACDE#ABC/W'];
var results = new Array();
for (var counter = 0; counter < test1.length; counter++ )
{
if( test1[counter].indexOf("W") >= 0 )
{
results[results.length] = "Element " + (counter + 1) + " is [" + test1[counter] + "] and contains 'W'";
continue;
}
var dashLocation = test1[counter].indexOf("#");
var threeChars = test1[counter].substr( dashLocation + 1, 3 );
var reg = /[Aa].[Cc]/;
if( reg.test(threeChars) )
{
results[results.length] = "Element " + (counter + 1) + " is [" + test1[counter] + "] and contains '" + threeChars + "'";
continue;
}
results[results.length] = "";
}
// Here are the results:
var resultsStr = "";
for( var i = 0; i < results.length; i++ )
{
resultsStr += (results[i] + (results[i].length > 0? "\n" : ""));
}
if( resultsStr.length > 0 )
alert(resultsStr);
</script>
</head>
<body>
</body>
</html>

[edited by: Fotiman at 9:32 pm (utc) on Feb. 6, 2007]