IRC String comparison is always true

124 Views Asked by At

I'm trying to simply compare a line in a text file to today's date. The line I want help with always seems to evaluate true for my code. Any examples?

My code:

set %lines $lines(test.txt)
set %date $adate

while (%i <= %lines)
  set %read $read(test.txt, n, %i)

  if( %date isin %read ){  ; <-- Line in question
    do things
  }
}
2

There are 2 best solutions below

3
Orel Eraki On

You have a few errors.

Your while loop is missing an open bracket. (And closing at the end)

while (%i <= %lines) {

You must have a space between () { } and the rest of the lines

if<space>(
)<space> {

if ( %date isin %read ) {

I took the liberty of suggesting another version.

Code:

  var %filename = test.txt
  var %lines = $lines(%filename)
  var %currentDate = $adate
  var %i = 1
  while (%i <= %lines) {
    var %line = $read(%filename, n, %i)
    if (%currentDate isin %line) {
      # do things

      # Should uncomment the break in case you want to stop after a match
      #break
    }
    inc %i
  }
1
Sirius_Black On

I am sorry if I didn't understand, but what is the reason for a complex script just to check if there is a date format in a line of a text file?

There is no reason to set a variable %read to store the line of the loop in question, when you can do an IF condition after the loop:

var %x = 1
while (%x <= $lines(test.txt)) { 
if ($adate isin $read(test.txt,n,%x)) {
 ;do things
}
inc %x
}