How to detect number

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
doscode
Posts: 175
Joined: 15 Feb 2012 14:02

How to detect number

#1 Post by doscode » 16 Jun 2012 01:17

I try to parse file and the need to detect number for every column in the tags.. I must know 1) if there is a number 2) if the number is 1-3 digits. 3) if there i a single dot at begin, end or separated dot.

I already have the for loop that extracts the data in tags:

Code: Select all

FOR %%Z IN (hide_2.htm) DO (
FOR /F "tokens=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 delims=<>" %%A IN ('grep -B 1411 -E "</table>" %%Z ^| grep -E ^"^(display^|^^\d\d{1,3}^|country^|^<td^>HTTP^|rightborder^).*$^" ') DO (
echo A:%%A + %%B + %%C + %%D + %%E + %%F + %%G + %%H + %%I + %%J + %%K + %%L
pause
)
)


A: + td + span + span + 41 + /span + span style="display: none;" + 111
+ /span + div + +

A: style="display: none;" + 190 + /div + span class="" style="" +. + /span + sp
an + 197 + /span + span + +

A: style="display: none;" + 24 + /span + span + /span + . + span style="display:
+ + + + +

A:inline;" + 132 + /span + span style="display: none;" + 39 + /span + . + span
+ + + +

A:style="display: inline;" + 186 + /span + /span + /td + + + + + +
+

A: + td rel="rw" + span class="country" + img + + + + + + + +

A: + td + HTTPS + /td + + + + + + + +


Source data are here:
http://codepaste.net/1kj4xb

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How to detect number

#2 Post by Ed Dyreen » 16 Jun 2012 09:26

'
try

Code: Select all

@echo off &setlocal enableDelayedExpansion

set "$=0" &if defined $ if !$! equ +!$! echo. isNumber: '!$!'
set "$=NaN" &if defined $ if !$! equ +!$! echo. isNumber: '!$!'

pause

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Re: How to detect number

#3 Post by doscode » 16 Jun 2012 09:53

Can it be like so?

Code: Select all

if defined E if %%E equ +%%E echo. isNumber: '%%E'


It work! Great. Thank you

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How to detect number

#4 Post by Ed Dyreen » 16 Jun 2012 10:09

doscode wrote:Can it be like so?

Code: Select all

if defined E if %%E equ +%%E echo. isNumber: '%%E'


It work! Great. Thank you
No it won't, it will crash when %%E is empty, 'E' is not the same as '%%E' :!:

try

Code: Select all

if "%%E" neq "" if %%E equ +%%E echo. isNumber: '%%E'
You are confusing variables '%E%' or '!E!' with tokens '%%E' or '%%~E' :wink:

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Re: How to detect number

#5 Post by doscode » 16 Jun 2012 10:11

What supernatural power edits my posts? Ok, but you didn't explain what the dolar means and I thought that the $ stands for variable name.

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Re: How to detect number

#6 Post by doscode » 16 Jun 2012 10:18

Ed Dyreen wrote:
doscode wrote:You are confusing variables '%E%' or '!E!' with tokens '%%E' or '%%~E' :wink:


You're right. I called it column or variable... Can you explain meaning of the $? I know this only from regex.
Last edited by doscode on 16 Jun 2012 10:47, edited 1 time in total.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How to detect number

#7 Post by Ed Dyreen » 16 Jun 2012 10:28

doscode wrote:Can you explain meaning of the $? I know this only from regex.
It doesn't has any 'special' meaning in DOS, it is the name of a variable, namely $, I could just as well have written

Code: Select all

set "var=0" &if defined var...

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Re: How to detect number

#8 Post by doscode » 16 Jun 2012 10:51

Ed Dyreen wrote:It doesn't has any 'special' meaning in DOS, it is the name of a variable, namely $, I could just as well have written

This is what I thought until you wrote that I have error in my code. I tested both variations with !E! and %%E and both seemed to me as working. $ is similar to JQuery, http://jquery.com/, where the $ means name of variable...

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Re: How to detect number

#9 Post by doscode » 16 Jun 2012 10:57

Yet I need to expand 1st character from the number to check if is there dot at begin. Can I do something like this?

Code: Select all

SET temp=%%%%E:~0,1%%
echo temp:!temp!

It doesn't seem that it would work for %%E

It seems to me like I have to save the token to variable every time when I want to do a string substitution...
Last edited by doscode on 16 Jun 2012 11:12, edited 1 time in total.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How to detect number

#10 Post by Ed Dyreen » 16 Jun 2012 11:05

doscode wrote:Yet I need to expand 1st character from the number to check if is there dot at begin. Can I do something like this?

Code: Select all

SET temp=%%%%E:~1,1%%
echo temp:!temp!

It doesn't seem that it would work for %%E
That don't work for tokens, only for variables :lol:

try

Code: Select all

@echo off &setlocal enableDelayedExpansion

set "$=NaN" &if "!$:~0,1!" == "." echo. hasDot: '!$!'
set "$=.NaN" &if "!$:~0,1!" == "." echo. hasDot: '!$!'
set "$=.%%E" &if "!$:~0,1!" == "." echo. hasDot: '!$!'
set "$=%%E" &if "!$:~0,1!" == "." echo. hasDot: '!$!'

pause

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Re: How to detect number

#11 Post by doscode » 16 Jun 2012 11:17

I have this simplified code:

Code: Select all

SET E=%%E
if "%%E" neq "." (
  SET first=!E:~0,1!
  if "first"=="." (
  SET number=!E:~1,0!
  ) else (
  SET number=%%E
  ) 
) else (
  SET dot=.
)
if defined number if !number! equ +!number! echo. isNumber: '!E!'


I hope there is not error.

Now I need to think up solution applicable for all columns. I guess it is not possible to use subroutine's errorlevel to save/return stings? Also it is not possible to use call :myfunction in for loop... So should I create new file for this code? As I need to simplify the code and return string dot;number
Last edited by doscode on 16 Jun 2012 11:42, edited 1 time in total.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How to detect number

#12 Post by Ed Dyreen » 16 Jun 2012 11:41

'
Not sure I understand,

Code: Select all

@echo off &setlocal enableDelayedExpansion

for /f "usebackq delims=" %%? in (

   "hide_2.htm"

) do    for /f "tokens=1-20 delims=<>" %%a in (

   'grep -B 1411 -E "</table>" "%%~?" ^|grep -E "(display|^\d\d{1,3}|country|<td>HTTP|rightborder).*$"'

) do    for %%? in (

   "%%~a", "%%~b", "%%~c", "%%~d", "%%~e", "%%~f", "%%~g", "%%~h", "%%~i", "%%~j"
   "%%~k", "%%~l", "%%~m", "%%~n", "%%~o", "%%~p", "%%~q", "%%~r", "%%~s", "%%~t"

) do    set "$=%%~?" &if "!$:~0,1!" == "." (

   (echo. !$!)
) else (echo. .!$!)

pause

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Re: How to detect number

#13 Post by doscode » 16 Jun 2012 11:47

Does the question mark mean the same as dollar? It will take some time till I will test it.

It is very interesting idea to do it this way. I would continue tomorrow. Thanks

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Re: How to detect number

#14 Post by doscode » 17 Jun 2012 04:01

Can you explain to me why this does not work?

Code: Select all

if defined second (
  SET ThisIsLegalNumber=0
  if !second! equ +!second! (
    if not defined firstNumber ( SET firstNumber=1 ) else (
       SET firstNumber=0
    )
    ECHO FIRST NUMBER:
    ECHO firstNumber: !firstNumber!
    ECHO $ is !$! , second is !second!

    if !firstNumber! equ 1 if "!$!" == "second" SET ThisIsLegalNumber=1
   
    if !firstNumber! equ 1 ECHO WORKS ... WORKS ... WORKS ... WORKS ...
    if "!firstNumber!"=="1" ECHO WORKS ... WORKS ... WORKS ... WORKS ...
   
    if !nextIsOK! equ 1 SET ThisIsLegalNumber=1
    if "!first!"=="." SET ThisIsLegalNumber=1   

    ECHO ThisIsLegalNumber: !ThisIsLegalNumber! 

  )
 
  )

This is code which I run inside the 3rd loop. I proceed all tokens. When first number appears, firstNumber is set to 1. In one of the next cycles the variable firstNumber is set to 0 in case that it is number. But these conditions don't work:

Code: Select all

    ECHO firstNumber: !firstNumber!
    if !firstNumber! equ 1 if "!$!" == "second" SET ThisIsLegalNumber=1
    if !firstNumber! equ 1 ECHO WORKS ... WORKS ... WORKS ... WORKS ...
    if "!firstNumber!"=="1" ECHO WORKS ... WORKS ... WORKS ... WORKS ...

For the first number the firstNumber: 1 is displayed. In the next are firstNumber: 0

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How to detect number

#15 Post by Ed Dyreen » 17 Jun 2012 06:38

'
if defined second (
SET ThisIsLegalNumber=0
if !second! equ +!second! (
if not defined firstNumber ( SET firstNumber=1 ) else (
SET firstNumber=0
)
ECHO FIRST NUMBER:
ECHO firstNumber: !firstNumber!
ECHO $ is !$! , second is !second!

if !firstNumber! equ 1 if "!$!" == "second" SET ThisIsLegalNumber=1

if !firstNumber! equ 1 ECHO WORKS ... WORKS ... WORKS ... WORKS ...
if "!firstNumber!"=="1" ECHO WORKS ... WORKS ... WORKS ... WORKS ...
ECHO ON :?:
if !nextIsOK! equ 1 SET ThisIsLegalNumber=1
if "!first!"=="." SET ThisIsLegalNumber=1

ECHO ThisIsLegalNumber: !ThisIsLegalNumber!

)

)

Post Reply