My Custom Rack I Built
Reply to topic
 
Regular Expression or array at fault?

Moderator

Moderator

Joined:25 Jan 2003
Posts:1312
Location:England
Reply with quote
Hi,

I have code that used to extract words using indexOf and substring. I have found that unreliable (due to the variable contents being read not having a consistent format). Therefore I now use a regular Expression which is accurate everytime.
Now where x="dog" and y="dog"
x==y is TRUE.

Now that I use a regular expression and save that retrieved info in an array, x does not equal y. Even though an alert() shows they contain the SAME info.

I have found that the info from the regExp, which is passed to an array is NOT a string which is why it does not equal true.

I have done myarray[0].toString and the now x and y are true when compared.

Finally to my question, why is the info in the regexp/array not a string, and which of these two is the culprit?

Thanks.
View user's profileFind all posts by batfinkSend private message
Regular Expression or array at fault?

Site Admin

Site Admin

Joined:30 Apr 2000
Posts:2841
Location:Texas, USA
Reply with quote
I'm trying to understand more about the context of the question.
Here is some sample code - is this even on the right topic? Smile

Code:

<html>
<head>
<script>
function compare()
{
  var _f=document.f;
  var _v1=_f.v1;
  var _v2=_f.v2;
  var _r=_f.result;
  var REG=new RegExp(_v2.value);
                                                                                                     
  var _s=""; /* contain the result string */
                                                                                                     
  if(_v1.value==_v2.value)
    _s+="String Compare=TRUE\n";
  else
    _s+="String Compare=FALSE\n";
                                                                                                     
  if(_v1.value.match(_v2.value))
    _s+="RegExp Compare=TRUE\n";
  else
    _s+="RegExp Compare=FALSE\n";
                                                                                                     
  _r.value=_s; /* show us the result */
}
</script>
</head>
<body>
<form name=f>
Value 1:<input type=text name=v1 value="dog"><BR>
Value 2:<input type=text name=v2 value="dog"><BR>
<input type=button value="Compare" onclick="compare()">
<HR>
Result:<textarea name=result>-run to see result</textarea>
</form>
</body>
</html>

_________________
Cheers.

-- Mike Robb

joker
View user's profileFind all posts by mikeSend private messageVisit poster's website
Regular Expression or array at fault?

Moderator

Moderator

Joined:25 Jan 2003
Posts:1312
Location:England
Reply with quote
What I have is a text file read in to a variable (using JAVA) called probs.
So for example:
probs="dog[cat<frog";
When the code worked I used:
To find cat I did
Code:
pos=probs.indexOf("[");
animal=prob.substring(pos+1,pos+3);

Then this code would be TRUE
Code:
if(animal=="cat"){do some code}


When the code did not work I used:
To find cat I did
Code:
var reggy = /\Da\D/;
myArray[0]=probs.match(reggy);

Then this code would be FALSE
Code:
if(myArray[0]=="cat"){do some code}


Does that make it any clearer?
View user's profileFind all posts by batfinkSend private message
Regular Expression or array at fault?

Site Admin

Site Admin

Joined:30 Apr 2000
Posts:2841
Location:Texas, USA
Reply with quote
I took what your last post and built this example a bit further.
When I run it the all pass back TRUE.
Any ideas on what you were doing differently?
Code:

<html>
<head>
<script>
function compare()
{
  var _f=document.f;
  var probs=_f.probs.value;
  var _r=_f.result;
                                                                                                     
  var _s=""; /* contain the result string */
                                                                                                     
  /* do indexOf */
  var pos=probs.indexOf("[");
  var animal=probs.substring(pos+1,pos+4);
  if(animal=="cat")
    _s+="INDEXOF Compare == cat\n";
  else
    _s+="INDEXOF Compare == ["+animal+"]\n";
                                                                                                     
  /* do regexp - no array */
  var reggy=new RegExp(/\Da\D/);
  var blah=probs.match(reggy);
  if(blah=="cat")
    _s+="RegExp Compare == cat\n";
  else
    _s+="RegExp Compare != cat\n";
                                                                                                     
  /* do regexp - with array */
  var blah2=new Array();
  blah2[0]=probs.match(reggy);
  if(blah2[0]=="cat")
    _s+="RegExp Array Compare == cat\n";
  else
    _s+="RegExp Array Compare != cat\n";
                                                                                                     
  for(var i=0;i<blah2.length;i++)
    _s+="blah2["+i+"]='"+blah2[i]+"'\n";
                                                                                                     
  _r.value=_s; /* show us the result */
}
</script>
</head>
<body>
<form name=f>
Value 1:<input type=text name=probs value="dog[cat<frog"><BR>
<input type=button value="Compare" onclick="compare()">
<HR>
Result:<textarea rows=10 cols=50 name=result>-run to see result</textarea>
</form>
</body>
</html>

_________________
Cheers.

-- Mike Robb

joker
View user's profileFind all posts by mikeSend private messageVisit poster's website
Regular Expression or array at fault?

Moderator

Moderator

Joined:25 Jan 2003
Posts:1312
Location:England
Reply with quote
Thanks for your time on this Mike.

Hmm this time I test I get true like you do ........ but now it's lead me to the cookie function. Confused
Ok I'll give the real code this time.
Code:
function find_prob()
{
fltcount=0,lflts=0;
probs="";cookieData="";
cookieData = document.cookie; // reads the cookie
try{
probs = window.parent.displayframe.document.readerApplet.sendtext(p); // this calls the java code that reads the file
}
catch(e)
{
alert(e + "\n\nFCO CIM MONITOR ERROR\n\nJava is not installed or your security settings\n are preventing it from running.");
window.parent.opener=null;
window.parent.close();
}

 var thealert = new Array(),i=0,Ncol=0;
 var output = "<body background='images/fadeb.jpg' topmargin='0'><center>";
 output += "<table><TR>";
 thealert[0] = new Array ("Failed","<img src='images/red.gif' width='32' height='32'>");
 thealert[1] = new Array ("Degraded","<img src='images/yellow.gif' width='32' height='32'>");
 thealert[2] = new Array ("Inaccessible","<img src='images/black.gif' width='32' height='32'>");
 var reggy = /\D\D\D\D\D0[1-9]|Ok|Degraded|Inaccessible|Failed/; // search string
 var theinfo = new Array();
 do
 {
  theinfo[i]=probs.match(reggy); // get the postname and status
  probs = probs.replace(theinfo[i],""); // delete entry from original string
  //if(theinfo[i]=="THHAZ02"){alert("true before conversion");}
  //if(theinfo[i]!=null){theinfo[i] = theinfo[i].toString();} // convert contents to a string
  //if(theinfo[i]=="THHAZ02"){alert("true after conversion");}
  i++;
 } while (theinfo[i-1]!=null)
 theinfo.length=theinfo.length-1;

 for(v=0;v<3;v++)
 {
  var p=0;
  do
  {
   if(thealert[v][0]==theinfo[p])
   {
    dpostname=theinfo[p+1];
    output += "<TD>";
    output += thealert[v][1];
    output += "</TD><TD><B>"
    output += thealert[v][0];
    output += "</B></TD><TD><B> : </B></TD><TD><B><font size='4'>";
    output += dpostname + "</font></B></TD>";
    output += "<TD align='center'><input type='checkbox' ";
    smref  = getcookie(dpostname);
.........................function continues......

the variable thepost is the original variable that's come via Java, through to the variable probs into the array theinfo[i] across to the variable dpostname. Shocked The cookie function is then called:
Code:
smref=getcookie(dpostname);

Code:
function getcookie(thepost)  // read the cookie
{
if(thepost=="THHAZ02"){alert("This always returns true");}
var labelLen = thepost.length;
var cLen = cookieData.length;
var i = 0;
var cEnd;
while(i < cLen)
{
 var j = i + labelLen;
 if(cookieData.substring(i,j)==thepost)
 {
  alert("code never gets here unless to.String is used");   
  cEnd = cookieData.indexOf(";",j);
  if(cEnd == -1)
  {
   cEnd = cookieData.length;
  }
 return(unescape(cookieData.substring(j+1,cEnd))); //returns the l char
 }
i++;
}
return "false";
}


Without my full code (which is huge) I guess you cannot test it. But a cookie is already set with the value THHAZ02 in it with the data l123456.
Cookie contents:
Code:
THHAZ02
l123456
~~local~~
1088
1289713408
29702627
2368217440
29702610
*

Sorry to have wasted time rambling about regexp and arrays but that what I thought was the problem. So basically the cookie is not read properly unless thepost is converted to a string.
View user's profileFind all posts by batfinkSend private message
Regular Expression or array at fault?
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum
All times are GMT - 6 Hours  
Page 1 of 1  


  
  
 Reply to topic