0

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
  }
}
Valentin Lorentz
  • 9,556
  • 6
  • 47
  • 69

2 Answers2

0

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
  }
Orel Eraki
  • 11,940
  • 3
  • 28
  • 36
0

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
}
Amit K Bist
  • 6,760
  • 1
  • 11
  • 26
Sirius_Black
  • 471
  • 3
  • 11
  • You understood as best you could. I'm still very green when it comes to IRC coding. I was just trying several different things to understand how they work. At the time I had no idea white-space was so picky (bracket errors aside) – MrJerkBird Jan 01 '17 at 03:35