AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

(Solved) Replacing no result with the number 0

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
rum



Joined: 03 Aug 2009
Posts: 42

PostPosted: Wed Feb 03, 2010 1:57 am    Post subject: (Solved) Replacing no result with the number 0 Reply with quote

As I data mine Google, I am wondering how to replace no results, with the number 0.

i.e. if there are no hits for say, John Biblotit on google books, there would be a zero, instead of that entry being blank.

how do you use RegExReplace to replace no result?

Thanks in advance!

Here is my existing code:

Code:
S := 1
L0 := 0

URLDownloadToFile, http://en.wikipedia.org/wiki/Category:Chinese_centenarians, wiki.txt
; shorter list Category:Chinese_centenarians
FileRead, Con, wiki.txt
While (Pos := RegExMatch(Con, "<li><a href=""/wiki/.*?"" title=""(\S.+?)"">", Sub, S)) { 
; original script: <ul><li><a href="/wiki/Hiroshi_Takeyasu" title="Hiroshi Takeyasu">Hiroshi Takeyasu</a></li></ul>
        L0++
NewStr := RegExReplace(Q, "\(.*?\)", "", count) ; removes the parenethis in a name.
; msgbox %NewStr% for testing
        Q := Sub1
        S := Pos + StrLen(Sub1) + 13


        URLDownloadToFile, http://news.google.com/archivesearch?q="%NewStr%"+&btnG=Search+Archives, google.txt
        FileRead, CCon, google.txt
        RegExMatch(CCon, "of (?:about )?<b>(\S+)</b>", Sub)
        L%A_Index%Results := RegExReplace(Sub1, "[^\d]")

;msgbox, %Q% "http://books.google.com/books?q=+"%NewStr%"+&btnG=Search+Books" ; for testing
        URLDownloadToFile, http://books.google.com/books?q=+"%Q%"+&btnG=Search+Books, google.htm
        FileRead, CCon1, google.htm
        RegExMatch(CCon1, "of (?:about )?<b>(\S+)</b>", Subl)
        n%A_Index%Results := RegExReplace(Subl1, "[^\d]")

;msgbox, %Q% "http://scholar.google.com/scholar?&q="%NewStr%"""" ; for testing
       URLDownloadToFile, http://scholar.google.com/scholar?&q="%Q%, google.htm
        FileRead, CCon1, google.htm
        RegExMatch(CCon1, "of (?:about )?<b>(\S+)</b>", Subl)
        m%A_Index%Results := RegExReplace(Subl1, "[^\d]")
       
      L%A_Index% := NewStr
}

L := "<span class=""plainlinks"">`r`n{|class=""wikitable sortable""`r`n!Name!!Google books!!Google news!!Google scholar"
;L := "Found " . L0 . " results.`r`n"
Loop %L0%
L .= "`r`n|-valign=top `r`n|align=left|[[" . L%A_Index%                               

         . "]]`n|align=center|<!--news-->[http://news.google.com/archivesearch?q={{urlencode:""" . L%A_Index% .

"""}}+&btnG=Search+Archives "  . L%A_Index%Results .                                

"]`n|align=center|<!--books-->[http://books.google.com/books?q=+{{urlencode:"""  . L%A_Index% . """}}+&btnG=Search+Books " . n%

A_Index%Results                                  . "]`n|align=center|<!-

-scholar--> [http://scholar.google.com/scholar?&q={{urlencode:""" . L%A_Index% . """}} " . m%A_Index%Results . "]"
;MsgBox %L% ; for testing
FileAppend, %L%, %a_scriptdir%\L.txt
Run, %a_scriptdir%\L.txt
Return


Last edited by rum on Wed Feb 03, 2010 8:24 pm; edited 1 time in total
Back to top
View user's profile Send private message
MasterFocus



Joined: 08 Apr 2009
Posts: 3035
Location: Rio de Janeiro - RJ - Brasil

PostPosted: Wed Feb 03, 2010 2:56 am    Post subject: Reply with quote

Maybe you could do something like this:
Code:
If ( Variable = "" )
  Variable := "0" ; quotation marks not really necessary

_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"

Antonio França
My stuff: Google Profile
Back to top
View user's profile Send private message Visit poster's website
[VxE]



Joined: 07 Oct 2006
Posts: 3254
Location: Simi Valley, CA

PostPosted: Wed Feb 03, 2010 7:02 am    Post subject: Reply with quote

Code:
RegexReplace( RegExReplace(Subl1, "[^\d]"), "^$", "0" )

_________________
Ternary (a ? b : c) guide     TSV Table Manipulation Library
Post code inside [code][/code] tags!
Back to top
View user's profile Send private message
rum



Joined: 03 Aug 2009
Posts: 42

PostPosted: Wed Feb 03, 2010 8:32 am    Post subject: didn't work, gave me strange results Reply with quote

[VxE] wrote:
Code:
RegexReplace( RegExReplace(Subl1, "[^\d]"), "^$", "0" )


It gave me some strange results, none of the actual numbers given are accurate, in addition there is an extra line at the beginning that wasnt there before.....

Results:

Code:
<span class="plainlinks">
{|class="wikitable sortable"
!Name!!Google books!!Google news!!Google scholar
|-valign=top
|align=left|[[]]
|align=center|<!--news-->[http://news.google.com/archivesearch?q={{urlencode:""}}+&btnG=Search+Archives 0]
|align=center|<!--books-->[http://books.google.com/books?q=+{{urlencode:""}}+&btnG=Search+Books 840]
|align=center|<!--scholar--> [http://scholar.google.com/scholar?&q={{urlencode:""}} 1550]
|-valign=top
|align=left|[[Ba Jin]]
|align=center|<!--news-->[http://news.google.com/archivesearch?q={{urlencode:"Ba Jin"}}+&btnG=Search+Archives 1550]
|align=center|<!--books-->[http://books.google.com/books?q=+{{urlencode:"Ba Jin"}}+&btnG=Search+Books 232]
|align=center|<!--scholar--> [http://scholar.google.com/scholar?&q={{urlencode:"Ba Jin"}} 25]
|-valign=top
|align=left|[[Bei Shizhang]]
|align=center|<!--news-->[http://news.google.com/archivesearch?q={{urlencode:"Bei Shizhang"}}+&btnG=Search+Archives 25]
|align=center|<!--books-->[http://books.google.com/books?q=+{{urlencode:"Bei Shizhang"}}+&btnG=Search+Books 0]
|align=center|<!--scholar--> [http://scholar.google.com/scholar?&q={{urlencode:"Bei Shizhang"}} 288]
|-valign=top
|align=left|[[Chang Ch'ün]]
|align=center|<!--news-->[http://news.google.com/archivesearch?q={{urlencode:"Chang Ch'ün"}}+&btnG=Search+Archives 288]
|align=center|<!--books-->[http://books.google.com/books?q=+{{urlencode:"Chang Ch'ün"}}+&btnG=Search+Books 643]
|align=center|<!--scholar--> [http://scholar.google.com/scholar?&q={{urlencode:"Chang Ch'ün"}} 381]
|-valign=top
|align=left|[[Chen Tuan]]
|align=center|<!--news-->[http://news.google.com/archivesearch?q={{urlencode:"Chen Tuan"}}+&btnG=Search+Archives 381]
|align=center|<!--books-->[http://books.google.com/books?q=+{{urlencode:"Chen Tuan"}}+&btnG=Search+Books 30]
|align=center|<!--scholar--> [http://scholar.google.com/scholar?&q={{urlencode:"Chen Tuan"}} 10]
|-valign=top
|align=left|[[Dazu Huike]]
|align=center|<!--news-->[http://news.google.com/archivesearch?q={{urlencode:"Dazu Huike"}}+&btnG=Search+Archives 10]
|align=center|<!--books-->[http://books.google.com/books?q=+{{urlencode:"Dazu Huike"}}+&btnG=Search+Books 262]
|align=center|<!--scholar--> [http://scholar.google.com/scholar?&q={{urlencode:"Dazu Huike"}} 57]
|-valign=top
|align=left|[[Jiang Ziya]]
|align=center|<!--news-->[http://news.google.com/archivesearch?q={{urlencode:"Jiang Ziya"}}+&btnG=Search+Archives 57]
|align=center|<!--books-->[http://books.google.com/books?q=+{{urlencode:"Jiang Ziya"}}+&btnG=Search+Books 1]
|align=center|<!--scholar--> [http://scholar.google.com/scholar?&q={{urlencode:"Jiang Ziya"}} 18]
|-valign=top
|align=left|[[Li Cairong]]
|align=center|<!--news-->[http://news.google.com/archivesearch?q={{urlencode:"Li Cairong"}}+&btnG=Search+Archives 18]
|align=center|<!--books-->[http://books.google.com/books?q=+{{urlencode:"Li Cairong"}}+&btnG=Search+Books 0]
|align=center|<!--scholar--> [http://scholar.google.com/scholar?&q={{urlencode:"Li Cairong"}} 39]
|-valign=top
|align=left|[[Lü Zhengcao]]
|align=center|<!--news-->[http://news.google.com/archivesearch?q={{urlencode:"Lü Zhengcao"}}+&btnG=Search+Archives 39]
|align=center|<!--books-->[http://books.google.com/books?q=+{{urlencode:"Lü Zhengcao"}}+&btnG=Search+Books 709]
|align=center|<!--scholar--> [http://scholar.google.com/scholar?&q={{urlencode:"Lü Zhengcao"}} 2390]
|-valign=top
|align=left|[[Run Run Shaw]]
|align=center|<!--news-->[http://news.google.com/archivesearch?q={{urlencode:"Run Run Shaw"}}+&btnG=Search+Archives 2390]
|align=center|<!--books-->[http://books.google.com/books?q=+{{urlencode:"Run Run Shaw"}}+&btnG=Search+Books 416]
|align=center|<!--scholar--> [http://scholar.google.com/scholar?&q={{urlencode:"Run Run Shaw"}} 31]
|-valign=top
|align=left|[[Soong May-ling]]
|align=center|<!--news-->[http://news.google.com/archivesearch?q={{urlencode:"Soong May-ling"}}+&btnG=Search+Archives 31]
|align=center|<!--books-->[http://books.google.com/books?q=+{{urlencode:"Soong May-ling"}}+&btnG=Search+Books 529]
|align=center|<!--scholar--> [http://scholar.google.com/scholar?&q={{urlencode:"Soong May-ling"}} 73]
|-valign=top
|align=left|[[Su Buqing]]
|align=center|<!--news-->[http://news.google.com/archivesearch?q={{urlencode:"Su Buqing"}}+&btnG=Search+Archives 73]
|align=center|<!--books-->[http://books.google.com/books?q=+{{urlencode:"Su Buqing"}}+&btnG=Search+Books 595]
|align=center|<!--scholar--> [http://scholar.google.com/scholar?&q={{urlencode:"Su Buqing"}} 510]
|-valign=top
|align=left|[[Sun Simiao]]
|align=center|<!--news-->[http://news.google.com/archivesearch?q={{urlencode:"Sun Simiao"}}+&btnG=Search+Archives 510]
|align=center|<!--books-->[http://books.google.com/books?q=+{{urlencode:"Sun Simiao"}}+&btnG=Search+Books 94]
|align=center|<!--scholar--> [http://scholar.google.com/scholar?&q={{urlencode:"Sun Simiao"}} 11]
|-valign=top
|align=left|[[Wu Yee-sun]]
|align=center|<!--news-->[http://news.google.com/archivesearch?q={{urlencode:"Wu Yee-sun"}}+&btnG=Search+Archives 11]
|align=center|<!--books-->[http://books.google.com/books?q=+{{urlencode:"Wu Yee-sun"}}+&btnG=Search+Books 697]
|align=center|<!--scholar--> [http://scholar.google.com/scholar?&q={{urlencode:"Wu Yee-sun"}} 3980]
|-valign=top
|align=left|[[Xiao Ke]]
|align=center|<!--news-->[http://news.google.com/archivesearch?q={{urlencode:"Xiao Ke"}}+&btnG=Search+Archives 3980]
|align=center|<!--books-->[http://books.google.com/books?q=+{{urlencode:"Xiao Ke"}}+&btnG=Search+Books 764]
|align=center|<!--scholar--> [http://scholar.google.com/scholar?&q={{urlencode:"Xiao Ke"}} 487]
|-valign=top
|align=left|[[Xue Muqiao]]
|align=center|<!--news-->[http://news.google.com/archivesearch?q={{urlencode:"Xue Muqiao"}}+&btnG=Search+Archives 487]
|align=center|<!--books-->[http://books.google.com/books?q=+{{urlencode:"Xue Muqiao"}}+&btnG=Search+Books 1142]
|align=center|<!--scholar--> [http://scholar.google.com/scholar?&q={{urlencode:"Xue Muqiao"}} 590]
|-valign=top
|align=left|[[Xue Yue]]
|align=center|<!--news-->[http://news.google.com/archivesearch?q={{urlencode:"Xue Yue"}}+&btnG=Search+Archives 590]
|align=center|<!--books-->[http://books.google.com/books?q=+{{urlencode:"Xue Yue"}}+&btnG=Search+Books 407]
|align=center|<!--scholar--> [http://scholar.google.com/scholar?&q={{urlencode:"Xue Yue"}} 107]
|-valign=top
|align=left|[[Zhou Youguang]]
|align=center|<!--news-->[http://news.google.com/archivesearch?q={{urlencode:"Zhou Youguang"}}+&btnG=Search+Archives 107]
|align=center|<!--books-->[http://books.google.com/books?q=+{{urlencode:"Zhou Youguang"}}+&btnG=Search+Books 664]
|align=center|<!--scholar--> [http://scholar.google.com/scholar?&q={{urlencode:"Zhou Youguang"}} 586]
|-valign=top
|align=left|[[Katherine Young]]
|align=center|<!--news-->[http://news.google.com/archivesearch?q={{urlencode:"Katherine Young"}}+&btnG=Search+Archives 586]
|align=center|<!--books-->[http://books.google.com/books?q=+{{urlencode:"Katherine Young"}}+&btnG=Search+Books 29]
|align=center|<!--scholar--> [http://scholar.google.com/scholar?&q={{urlencode:"Katherine Young"}} 23]
|-valign=top
|align=left|[[Sun Yuanliang]]
|align=center|<!--news-->[http://news.google.com/archivesearch?q={{urlencode:"Sun Yuanliang"}}+&btnG=Search+Archives 23]
|align=center|<!--books-->[http://books.google.com/books?q=+{{urlencode:"Sun Yuanliang"}}+&btnG=Search+Books 638]
|align=center|<!--scholar--> [http://scholar.google.com/scholar?&q={{urlencode:"Sun Yuanliang"}} 226]
|-valign=top
|align=left|[[Zhang Daoling]]
|align=center|<!--news-->[http://news.google.com/archivesearch?q={{urlencode:"Zhang Daoling"}}+&btnG=Search+Archives 226]
|align=center|<!--books-->[http://books.google.com/books?q=+{{urlencode:"Zhang Daoling"}}+&btnG=Search+Books 730]
|align=center|<!--scholar--> [http://scholar.google.com/scholar?&q={{urlencode:"Zhang Daoling"}} 784]
|-valign=top
|align=left|[[Zhang Xueliang]]
|align=center|<!--news-->[http://news.google.com/archivesearch?q={{urlencode:"Zhang Xueliang"}}+&btnG=Search+Archives 784]
|align=center|<!--books-->[http://books.google.com/books?q=+{{urlencode:"Zhang Xueliang"}}+&btnG=Search+Books 115]
|align=center|<!--scholar--> [http://scholar.google.com/scholar?&q={{urlencode:"Zhang Xueliang"}} 30]
|-valign=top
|align=left|[[Zhaozhou Congshen]]
|align=center|<!--news-->[http://news.google.com/archivesearch?q={{urlencode:"Zhaozhou Congshen"}}+&btnG=Search+Archives 30]
|align=center|<!--books-->[http://books.google.com/books?q=+{{urlencode:"Zhaozhou Congshen"}}+&btnG=Search+Books 909]
|align=center|<!--scholar--> [http://scholar.google.com/scholar?&q={{urlencode:"Zhaozhou Congshen"}} 0]
|-valign=top
|align=left|[[Zheng Ji ]]
|align=center|<!--news-->[http://news.google.com/archivesearch?q={{urlencode:"Zheng Ji "}}+&btnG=Search+Archives 0]
|align=center|<!--books-->[http://books.google.com/books?q=+{{urlencode:"Zheng Ji "}}+&btnG=Search+Books 20]
|align=center|<!--scholar--> [http://scholar.google.com/scholar?&q={{urlencode:"Zheng Ji "}} 0]
|-valign=top
|align=left|[[Joseph Meng Ziwen]]
|align=center|<!--news-->[http://news.google.com/archivesearch?q={{urlencode:"Joseph Meng Ziwen"}}+&btnG=Search+Archives 0]
|align=center|<!--books-->[http://books.google.com/books?q=+{{urlencode:"Joseph Meng Ziwen"}}+&btnG=Search+Books 370]
|align=center|<!--scholar--> [http://scholar.google.com/scholar?&q={{urlencode:"Joseph Meng Ziwen"}} 74]


MasterFocus wrote:
Maybe you could do something like this:
Code:
If ( Variable = "" )
  Variable := "0" ; quotation marks not really necessary


I tried

Quote:
If ( L = "" )
L := 0 ; quotation marks not really necessary


didn't work, probably doing it wrong, thanks though!


Last edited by rum on Wed Feb 03, 2010 8:45 am; edited 2 times in total
Back to top
View user's profile Send private message
Murx
Guest





PostPosted: Wed Feb 03, 2010 8:36 am    Post subject: Reply with quote

Check out URLDownloadToVar too!

Code:
MyVar := ""
Status := MyVar = "" ? 0:MyVar
MsgBox % Status
Back to top
nick



Joined: 24 Aug 2005
Posts: 549
Location: Berlin / Germany

PostPosted: Wed Feb 03, 2010 10:05 am    Post subject: Reply with quote

Code:
; ...
NewStr := RegExReplace(Sub1, "\(.*?\)", "", count) ; removes the parenethis in a name.
; ...
L%A_Index%Results := RegexReplace(RegExReplace(Sub1, "\D"), "^$", "0")
; ...
n%A_Index%Results := RegexReplace(RegExReplace(Subl1, "\D"), "^$", "0")
; ...
m%A_Index%Results := RegexReplace(RegExReplace(Subl1, "\D"), "^$", "0")
; ...

_________________
nick Wink
Back to top
View user's profile Send private message
rum



Joined: 03 Aug 2009
Posts: 42

PostPosted: Wed Feb 03, 2010 6:09 pm    Post subject: problem solved with adding 0 to no results Reply with quote

nick wrote:
Code:
; ...
NewStr := RegExReplace(Sub1, "\(.*?\)", "", count) ; removes the parenethis in a name.
; ...
L%A_Index%Results := RegexReplace(RegExReplace(Sub1, "\D"), "^$", "0")
; ...
n%A_Index%Results := RegexReplace(RegExReplace(Subl1, "\D"), "^$", "0")
; ...
m%A_Index%Results := RegexReplace(RegExReplace(Subl1, "\D"), "^$", "0")
; ...


Ha it worked! Good to see you again Nick! Thank you everyone for your suggestions!

Code:
f8::
S := 1
L0 := 0

URLDownloadToFile,http://en.wikipedia.org/w/index.php?title=Category:Unreferenced_BLPs_from_April_2007&from=Li+Na, wiki.txt
; shorter list Category:Chinese_centenarians
FileRead, Con, wiki.txt
While (Pos := RegExMatch(Con, "<li><a href=""/wiki/.*?"" title=""(\S.+?)"">", Sub, S)) { 
; original script: <ul><li><a href="/wiki/Hiroshi_Takeyasu" title="Hiroshi Takeyasu">Hiroshi Takeyasu</a></li></ul>
        L0++
        Q := Sub1
        S := Pos + StrLen(Sub1) + 13
NewStr := RegExReplace(Q, "\(.*?\)", " ", count) ; removes the parenthesis in a name.
; msgbox %NewStr% for testing

        URLDownloadToFile, http://news.google.com/archivesearch?q="%NewStr%"+&btnG=Search+Archives, google.txt
        FileRead, CCon, google.txt
        RegExMatch(CCon, "of (?:about )?<b>(\S+)</b>", Sub)
        L%A_Index%Results := RegexReplace(RegExReplace(Sub1, "\D"), "^$", "0")

;msgbox, %Q% "http://books.google.com/books?q=+"%NewStr%"+&btnG=Search+Books" ; for testing
        URLDownloadToFile, http://books.google.com/books?q=+"%Q%"+&btnG=Search+Books, google.htm
        FileRead, CCon1, google.htm
        RegExMatch(CCon1, "of (?:about )?<b>(\S+)</b>", Subl)
        n%A_Index%Results := RegexReplace(RegExReplace(Subl1, "\D"), "^$", "0")

;msgbox, %Q% "http://scholar.google.com/scholar?&q="%NewStr%"""" ; for testing
       URLDownloadToFile, http://scholar.google.com/scholar?&q="%Q%, google.htm
        FileRead, CCon1, google.htm
        RegExMatch(CCon1, "of (?:about )?<b>(\S+)</b>", Subl)
        m%A_Index%Results := RegexReplace(RegExReplace(Subl1, "\D"), "^$", "0")
       
      L%A_Index% := NewStr
}

L := "<span class=""plainlinks"">`r`n{|class=""wikitable sortable""`r`n!Name!!Google news!!Google books!!Google scholar"
;L := "Found " . L0 . " results.`r`n"
Loop %L0%
L .= "`r`n|-valign=top `r`n|align=left|[[" . L%A_Index% . "]]`n|align=center|<!--news-->[http://news.google.com/archivesearch?q={{urlencode:""" . L%A_Index% . """}}+&btnG=Search+Archives "  . L%A_Index%Results .  "]`n|align=center|<!--books-->[http://books.google.com/books?q=+{{urlencode:"""  . L%A_Index% . """}}+&btnG=Search+Books " . n%A_Index%Results  . "]`n|align=center|<!--scholar--> [http://scholar.google.com/scholar?&q={{urlencode:""" . L%A_Index% . """}} " . m%A_Index%Results . "]"
;MsgBox %L% ; for testing
FileAppend, %L%, %a_scriptdir%\L.txt
Run, %a_scriptdir%\L.txt
Return
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group