Author Topic: Greater than or equal to...  (Read 12088 times)

0 Members and 1 Guest are viewing this topic.

Water Bear

  • Guest
Greater than or equal to...
« on: April 15, 2004, 04:54:06 PM »
I'm trying to read an external file that requires a number to fall within a range using this list as an example
Quote
*****          test file  (gage,stock,slips)             *****
*12
26.0,96.0,0.5
*31
24.0,60.0,1.75
*54
22.0,60.0,1.75
*72
20.0,30.0,1.75
*100
18.0,30.0,1.75

And using this code...
Code: [Select]
(defun shop_spec (/); item data dline maxs count chrct numb size)
  (setq size  (max 60.0 36.0 28.0 24.0) ;<==sample sizes
dlist nil
file  (findfile "specs.dat")
fp    (open file "r")
item  (read-line fp)
) ;setq

  (while item
    (setq item (atof (substr item 2)))
    (if (>= item size );<====== this is the problem, I believe
      (setq data (read-line fp)
   item nil
   ) ;setq
      (setq item (read-line fp))
      ) ;if
    )...yadayada)

if the largest size is 60.0 it should be reading the line immediately following the *54 (because it falls between 54 and 72)... but it doesn't it skips to the next line (because 54 is NOT greaterthan or equal), however if I reverse those two it will read the first line it comes to (*12)  :shock:

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
Greater than or equal to...
« Reply #1 on: April 15, 2004, 06:10:24 PM »
I changed your code a little, but I believe it's doing what you're asking it to do.
Code: [Select]

(defun shop_spec (/ size dlist file fp line item data)

   (setq size  (max 60.0 36.0 28.0 24.0) ;<==sample sizes
         dlist nil
         file  (findfile "specs.dat")
         fp    (open file "r")
         ) ;setq

   (while
     (setq line (read-line fp))

      (setq item (atof (substr line 2)))
      (if (>= item size) ;<====== this is the problem, I believe
        (setq data (read-line fp)); <-- where i'm confused
        ) ;if

     (if data
        (princ (strcat "\n" data))
        )
     
      (princ)
      ) ; while
   (close fp)
   )
TheSwamp.org  (serving the CAD community since 2003)

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
Greater than or equal to...
« Reply #2 on: April 15, 2004, 06:36:56 PM »
Re-reading your post.......... I think you need to store the previous line each time through the while statement.
TheSwamp.org  (serving the CAD community since 2003)

Water Bear

  • Guest
Greater than or equal to...
« Reply #3 on: April 15, 2004, 06:38:41 PM »
Maybe I should post the entire code.
Code: [Select]
(defun C:Test (/ item data dline maxs count chrct numb size)
  (setq size  (max 60.0 36.0 28.0 24.0) ;<==sample sizes
dlist nil
file  (findfile "specs.dat")
fp    (open file "r")
item  (read-line fp);<==just to hold file header
) ;setq

  (while item
    (setq item (atof (substr item 2)))
    (if (>= item size) ;<================== here
      (setq data (read-line fp)
   item nil
   ) ;setq
      (setq item (read-line fp))
      ) ;if
    ) ;while
  (if data
    (progn
      (setq maxs  (strlen data)
   count 1
   chrct 1
   ) ;setq
      (while (< count maxs)
(if (/= "," (substr data count 1))
 (setq chrct (1+ chrct))
 (setq numb  (atof
(substr data
(1+ (- count chrct))
chrct
)
)
dlist (append dlist (list numb))
chrct 1
) ;setq
 ) ;if
(setq count (1+ count))
) ;while
      (setq numb  (atof
   (substr data
   (1+ (- count chrct))
   )
   )
   dlist (append dlist (list numb))
   ) ;setq
      ) ;progn
    ) ;if data
  (close fp)
  (mapcar 'set '(a b c) dlist)
  )
This is what I had originally. The last line should set three variables from the list (in this case the ones listed in betweem *54 and *72

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
Greater than or equal to...
« Reply #4 on: April 15, 2004, 06:49:17 PM »
Quote

*54
22.0,60.0,1.75  <-- is this what you're after ?
*72
TheSwamp.org  (serving the CAD community since 2003)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Greater than or equal to...
« Reply #5 on: April 15, 2004, 07:33:54 PM »
Code: [Select]
(defun shop_spec (/ size dlist file fp line item data)

  (setq size  (max 60.0 36.0 28.0 24.0) ;<==sample sizes
        dlist nil
        file  (findfile "specs.dat")
        fp    (open file "r")
        loop  t
  ) ;setq

  (while loop
    (cond
      ((setq line (read-line fp)) ; not eof
       (cond
         ((= (substr line 1 2) "**") ; header line
          ;;  do nothing
         )
         ((= (substr line 1 1) "*") ; flag line so test it
          (if (>= (atof (substr line 2)) size) ; we passed the dataline
            (progn
              (if (setq data lastline) ; not nil
                (setq loop nil) ; exit loop
                (alert "/nERROR size < smallest flag.")
              )
            )
          )
         )
         (t ; this is a data line, so save it
          (setq dataline line)
         )
       ) ; end cond stmt
      )
      (t
       (alert "/nERROR end of file.")
       (setq loop nil)
      )
    ) ; end cond stmt
  ) ; while
  (close fp)
  data ; return data      
) ; end defun
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Water Bear

  • Guest
Greater than or equal to...
« Reply #6 on: April 15, 2004, 08:09:22 PM »

Thanks for the help guys, but I have to allow for my own thickness...I still can't get it to work!
Quote
***** test file (gage,stock,slips) *****
*12
26.0,96.0,0.5
*31
24.0,60.0,1.75
*54
22.0,60.0,1.75
*72
20.0,30.0,1.75
*100
18.0,30.0,1.75
I'm trying to set this up:
1-a duct less or equal to 12 is made of 26 ga., 96 in long, 1/2 in for connection
2-a duct greater than 12 but less than 31 24ga., 60 in long, 1.75 for connection
3-a duct greater than 31 but less than 54 22ga., 60 in long, 1.75 for connection
4-a duct greater than 54 but less than 72 20ga., 60 in long, 1.75 for connection
5-anything larger than 72 18ga., 30 in long, 1.75 for connections

In the sample, the largest size is 60.0, therefore we should use option 4 ,but because of this part of the code:
Quote
(while item
    (setq item (atof (substr item 2)))
    (if   (>= item size)         ;<================== here
      (setq data (read-line fp)
       item nil
       )            ;setq
      (setq item (read-line fp))
      )               ;if
    )        
It skips to the next line and reads the *72 line

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Greater than or equal to...
« Reply #7 on: April 15, 2004, 09:19:00 PM »
This has the data line first, before the 12

Code: [Select]
***** test file (gage,stock,slips) *****
26.0,96.0,0.5
*12  
24.0,60.0,1.75
*31
22.0,60.0,1.75
*54
20.0,30.0,1.75
*72
18.0,30.0,1.75
*100


Code: [Select]
(defun shop_spec (size / dlist file fp line item data)

  (setq dlist nil
        file  (findfile "specs.dat")
        fp    (open file "r")
        loop  t
  ) ;setq

  (while loop
    (cond
      ((setq line (read-line fp)) ; not eof
       (cond
         ((= (substr line 1 2) "**") ; header line
          ;;  do nothing
         )
         ((= (substr line 1 1) "*") ; flag line so test it
          (if (>= (atof (substr line 2)) size); we passed the dataline
            (progn
              (if (setq data dataline) ; not nil
                (setq loop nil) ; exit loop
                (setq data (read-line fp))
              )
            )
          )
         )
         (t ; this is a data line, so save it
          (setq dataline line)
         )
       ) ; end cond stmt
      )
      (t
       (alert "/nERROR end of file.")
       (setq loop nil)
      )
    ) ; end cond stmt
  ) ; while
  (close fp)
  data ; return data      
) ; end defun


(defun c:test ()
  (foreach sz (list 60.0 36.0 28.0 24.0)
    (princ (strcat "\n" (rtos sz 2 1) " = " (shop_spec sz)))
  )
  (princ)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Greater than or equal to...
« Reply #8 on: April 15, 2004, 09:34:57 PM »
This way has the data line after the 12


Code: [Select]
***** test file (gage,stock,slips) *****
*12
26.0,96.0,0.5
*31
24.0,60.0,1.75
*54
22.0,60.0,1.75
*72
20.0,30.0,1.75
*100
18.0,30.0,1.75


Code: [Select]
(defun shop_spec (size / dlist file fp line item data)

  (setq dlist nil
file  (findfile "specs.dat")
fp    (open file "r")
loop  t
  ) ;setq

  (while loop
    (cond
      ((setq line (read-line fp)) ; not eof
       (cond
((= (substr line 1 2) "**") ; header line
 ;;  do nothing
)
((= (substr line 1 1) "*") ; flag line so test it
 (if (>= (atof (substr line 2)) size); max found
   (progn
     (setq loop nil) ; exit loop
     (setq data (read-line fp))
   )
 )
)
       ) ; end cond stmt
      )
      (t
       (alert "/nERROR end of file.")
       (setq loop nil)
      )
    ) ; end cond stmt
  ) ; while
  (close fp)
  data ; return data      
) ; end defun


(defun c:test ()
  (foreach sz (list 10.0 60.0 36.0 28.0 75.0)
    (princ (strcat "\n" (rtos sz 2 1) " = " (shop_spec sz)))
  )
  (princ)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Water Bear

  • Guest
Greater than or equal to...
« Reply #9 on: April 15, 2004, 09:39:18 PM »
Where does the variable "dataline" come from?
Quote
(cond
      ((setq line (read-line fp)) ; not eof
       (cond
         ((= (substr line 1 2) "**") ; header line
          ;;  do nothing
         )
         ((= (substr line 1 1) "*") ; flag line so test it
          (if (>= (atof (substr line 2)) size); we passed the dataline
            (progn
              (if (setq data dataline) ; not nil;<== dataline??
                (setq loop nil) ; exit loop
                (setq data (read-line fp))
              )
            )
          )
         )
         (t ; this is a data line, so save it
          (setq dataline line)
         )
       ) ; end cond stmt

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Greater than or equal to...
« Reply #10 on: April 15, 2004, 09:57:22 PM »
It is used in the first routine to hold the dataline before the flag line

Code: [Select]
(while loop
    (cond
      ((setq line (read-line fp)) ; not eof
       (cond
         ((= (substr line 1 2) "**") ; header line
          ;;  do nothing
         )
         ((= (substr line 1 1) "*") ; flag line so test it
          (if (>= (atof (substr line 2)) size); we passed the dataline
            (progn
              (if (setq data dataline) ; not nil
                (setq loop nil) ; exit loop
                (setq data (read-line fp))
              )
            )
          )
         )
         (t ; this is a data line, so save it
          (setq dataline line)  ; <<<===== HERE is where it is updated
         )
       ) ; end cond stmt
      )
      (t
       (alert "/nERROR end of file.")
       (setq loop nil)
      )
    ) ; end cond stmt
  ) ; while
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Water Bear

  • Guest
Greater than or equal to...
« Reply #11 on: April 15, 2004, 10:22:58 PM »
So you're saying that I have to run something like the first routine to hold the dataline, then run the second to trap it?

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Greater than or equal to...
« Reply #12 on: April 15, 2004, 11:28:53 PM »
Nice chicken..

I should have been more clear.

The last routine expects the data file like this:

***** test file (gage,stock,slips) *****
*12
26.0,96.0,0.5
*31
24.0,60.0,1.75


Note that the data line follows the code flag line *12 followed by 26.0,96.0,0.5
====================================================================================

The routine before that expects the data line to be before the code flag like this:

***** test file (gage,stock,slips) *****
26.0,96.0,0.5
*12  
24.0,60.0,1.75
*31

Note that the code line follows the data line 26.0,96.0,0.5 followed by *12

Just two options & how the data format affects the coding.
I got carried away, what can I say...

CAB
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Greater than or equal to...
« Reply #13 on: April 15, 2004, 11:58:44 PM »
Last one :)

Data format:

Code: [Select]
***** test file (gage,stock,slips) *****
***  Ducts < 12 inch use the data line following the 12
*12
26.0,96.0,0.5
*31
24.0,60.0,1.75
*54
22.0,60.0,1.75
*72
20.0,30.0,1.75
*100
18.0,30.0,1.75


Code: [Select]
(defun shop_spec (size / dlist file fp line item data)
  (setq dlist nil
        file  (findfile "specs.dat")
        fp    (open file "r")
        loop  t
  ) ;setq

  (while loop
    (cond
      ((setq line (read-line fp)) ; not eof
       (cond
         ((= (substr line 1 2) "**") ; header line
          ;;  do nothing
         )
         ((= (substr line 1 1) "*") ; flag line so test it
          (if (> (atof (substr line 2)) size) ; max found
            (progn
              (setq loop nil) ; exit loop
              (setq data (read-line fp))
            )
          )
         )
       ) ; end cond stmt
      )
      (t
       (alert "/nERROR end of file.")
       (setq loop nil)
      )
    ) ; end cond stmt
  ) ; while
  (close fp)
  data ; return data      
) ; end defun


(defun str2list (str pat / i j n lst)
  (cond
    ((/= (type str) (type pat) 'str))
    ((= str pat) '(""))
    (t
     (setq i 0
           n (strlen pat)
     )
     (while (setq j (vl-string-search pat str i))
       (setq lst (cons (substr str (1+ i) (- j i)) lst)
             i   (+ j n)
       )
     )
     (reverse (cons (substr str (1+ i)) lst))
    )
  )
)


;;================================================================

(defun c:test ()
  (foreach sz (list 12.0 60.0 36.0 28.0 78.0)
    (setq dstr  (shop_spec sz)
          dlist (str2list dstr ",")
    )
    (mapcar 'set '(a b c) dlist)
    (princ (strcat "\n" (rtos sz 2 1) " = " dstr))
  )
  (princ)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

NoLisper

  • Guest
Greater than or equal to...
« Reply #14 on: April 16, 2004, 05:10:07 AM »
Here's how I'll do it using a data file.
Can also be done without using a data file,
by inputting the data in the cond section.

Header line (*12 etc) must not have an extra
space before or after.

Code: [Select]


(defun getdata (xsize / datalist df found fp line)
  (setq str
    (cond
      ((<= xsize 12) "*12")
      ((< 12 xsize 32) "*31")    
      ((< 31 xsize 55) "*54")    
      ((< 54 xsize 73) "*72")    
      (T "*100")
    )
    df  (findfile "specs.dat")
    fp    (open df "r")
    datalist ""
    found nil
  )
  (while (and (not found)
              (setq line (read-line fp))
         )
    (if (= line str)
      (setq datalist (strcat datalist (read-line fp))
            found T
      )
      (if (and found fp) (close fp))
    )
  )
  (if (and fp (not found)) (close fp))
  datalist
)