Announcement

Collapse
No announcement yet.

The Programming Thread (was New to Programming)

Collapse
This is a sticky topic.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    #61
    I'd use the google calculator API:



    The examples there are in PHP. Dunno what the VBS equivalent is.

    Scripting's not my thing at all though so hopefully someone else can help.

    So you know how to update once every 5 minutes without using loads of resources? I'm just thinking that may be part of the test, you know, making sure people don't code a tight loop for example.
    Oh, and good luck.

    Comment


      #62
      That is extremely helpful, thank you very much

      Comment


        #63
        That Google API is great, reckon I'll knock something up in C# too, using webclient and expressions, and give them two programs.

        Code:
        using System;
        using System.Text.RegularExpressions; 
        
        public static class CurrencyConverter 
        {
        	public static String Convert(decimal amount, string fromCurr, string toCurr) 
        	{
                webClient web = newWebClient();  
        
                string url = string.Format("http://www.google.com/ig/calculator?hl=en&q={0}{1}=?{2}", amount, fromCurr.ToUpper(), toCurr.ToUpper());
                String response = web.DownloadString(url);
        
                Regex regex = new Regex ("rhs: \\\"(\\d*.\\d*)"); 
                Match match = regex.Match(response);  found
        
                return System.Convert.ToDecimal(match.Value); 
        	}
        }

        Comment


          #64
          I hate scripting languages too, people seem to rave about Python but the designers honestly thought it would make programs clearer if they based code blocks on white space indentation rather than using some explicit delimiting symbol. Makes it an absolute nightmare to follow long blocks of code, not quite as bad as Perl but still an evil decision in my opinion.

          Comment


            #65
            Scripting is giving me a headache, wish I had more time to learn this stuff. So far in VBS I've come up with:

            Code:
            Function dollar()
            	Dim http_req, pos
            	http_req = CreateObject("Msxml2.ServerXMLHTTP.3.0")
            	http_req.open "GET", "http://uk.finance.yahoo.com/q?s=GBPUSD=X&d=0b",False
            	http_req.send()
            	pos = instr(http_req.responseText, "")
            	dollar = mid(http_req.responseText,pos+54,6)
            End Function
            Which sends a request to this page and feed back the result. I keep getting an Error though (Cannot use parentheses when calling a Sub). Any ideas?

            EDIT: Unbelievable, I missed the letter "c" out of Function
            Last edited by Rep; 19-07-2011, 13:51.

            Comment


              #66
              Fixed my early problems and feels like I've had my first major break-through. The request is sent and writes the entire page in .html format. I need to filter and present to information now, any suggestions, there aren't any scripters about to give advice?

              Trying to Use Regular Expressions but confusing as hell, not really working for me.

              Code:
              Set objArgs = WScript.Arguments
              	
              	Dim oStream
              	Dim http_req
              	Set http_req = CreateObject("Msxml2.ServerXMLHTTP.3.0")
              	http_req.open "GET", "http://uk.finance.yahoo.com/q?s=GBPUSD=X&d=0b", False
              	http_req.send
              	
              If http_req.status = 200 Then	
              	Set oStream = CreateObject("ADODB.Stream")
              	oStream.Open
              	oStream.Type = 1
              	oStream.Write http_req.responseBody
              	oStream.SaveToFile "C:\Logfile.html"
              	oStream.Close
              End If
              
              'Regular Expression Object
              Function RegExpOne(patrn, strng)
              	Dim myRegExp, Match, Matches
              	Set myRegExp = New RegExp
              	myRegExp.IgnoreCase = True
              	myRegExp.Global = True
              	myRegExp.Pattern = patrn
              	Set Matches = myRegEx.Execute(strng)   
                 For Each Match in Matches   
                    RetStr = RetStr & "Match found at position "
                    RetStr = RetStr & Match.FirstIndex & ". /yfs_l10_gbpusd=x/i'"
                    RetStr = RetStr & Match.Value & "'." & vbCRLF
                 Next
                 RegExpTest = RetStr
                 MsgBox(RegExpTest)
              	
              	Set http_req = Nothing
              End Function
              Last edited by Rep; 19-07-2011, 19:51.

              Comment


                #67
                Bloody hell mate, you don't want to be dealing with n entire page of html. What went wrong with the google API? The output from that can be easily parsed.

                Sorry, I hit post before I'd finished typing...

                The point of a published API is that it shouldn't change. So, using the google API your code works right now and continues to work. If Yahoo mess around with that webpage your code could stop working mate. You don't have to use the google API but I'd definitely avoid having to parse an entire html page.
                Last edited by Brad; 19-07-2011, 21:55.

                Comment


                  #68
                  Does VBS have an explode function? explode the html with <span id="yfs_l10_gbpusd=x"> as the delimiter, then explode explodeoutputvariable[1] with </span> as the delimiter and [0] will contain the currency.

                  But yeah, what FSW said, it's pretty unlikely they want you to use a full HTML page to get the values

                  Comment


                    #69
                    I'll take another look at it, since I havn't used VBS before this I just kept following what works, using expressions seemed like a good way to grab the values

                    I think with the calculator I lost track of what I needed to do and started making a calculator, when the goal is just to retrieve a value, update it and store it.

                    Explode in VBS is Split.
                    Last edited by Rep; 20-07-2011, 08:32.

                    Comment


                      #70
                      I've just clicked and it all makes sense now I know exactly how to approach and finish it using the API, as usual I made it more complicated that it was.

                      Comment


                        #71
                        Less than a day to go, and so close to finishing this now Having a problem with my loop at the top, the code "seems" right and I'm not getting any errors, but nothing is happening. It's probably something I've forgotten to do, my brain has literally melted at this point.

                        Code:
                        [FONT=Courier New][COLOR=#0000FF]Set[/COLOR][COLOR=#000000] objArgs[/COLOR][COLOR=#000000] = WScript.Arguments[/COLOR]
                        
                        [COLOR=#0000FF]Do[/COLOR][COLOR=#000000] [/COLOR][COLOR=#0000FF]While[/COLOR][COLOR=#000000] [/COLOR][COLOR=#0000FF]True[/COLOR]
                        [COLOR=#000000]    [/COLOR][COLOR=#0000FF]Dim[/COLOR][COLOR=#000000] oStream[/COLOR]
                        [COLOR=#000000]    [/COLOR][COLOR=#0000FF]Dim[/COLOR][COLOR=#000000] http_req[/COLOR]
                        [COLOR=#000000]    [/COLOR][COLOR=#0000FF]Set[/COLOR][COLOR=#000000] http_req[/COLOR][COLOR=#000000] = [/COLOR][COLOR=#0000FF]CreateObject[/COLOR][COLOR=#000000]([/COLOR][COLOR=#808080]"Msxml2.ServerXMLHTTP.3.0"[/COLOR][COLOR=#000000])[/COLOR]
                        [COLOR=#000000]    http_req.open[/COLOR][COLOR=#000000] [/COLOR][COLOR=#808080]"GET"[/COLOR][COLOR=#000000], [/COLOR][COLOR=#808080]"http://www.google.com/ig/calculator?hl=en&q=1USD=?GBP"[/COLOR][COLOR=#000000], [/COLOR][COLOR=#0000FF]False[/COLOR]
                        [COLOR=#000000]    http_req.send[/COLOR]
                        [COLOR=#000000]    [/COLOR]
                        [COLOR=#000000]    [/COLOR][COLOR=#0000FF]If[/COLOR][COLOR=#000000] http_req.status[/COLOR][COLOR=#000000] = [/COLOR][COLOR=#800000]200[/COLOR][COLOR=#000000] [/COLOR][COLOR=#0000FF]Then[/COLOR]
                        [COLOR=#000000]        WScript.Sleep[/COLOR][COLOR=#000000] [/COLOR][COLOR=#800000]1000[/COLOR][COLOR=#000000]    [/COLOR]
                        [COLOR=#000000]    [/COLOR][COLOR=#0000FF]End[/COLOR][COLOR=#000000] [/COLOR][COLOR=#0000FF]If[/COLOR]
                        [COLOR=#000000]    [/COLOR][COLOR=#0000FF]Loop[/COLOR]
                        [COLOR=#000000]    [/COLOR]
                        [COLOR=#000000]    [/COLOR][COLOR=#0000FF]Set[/COLOR][COLOR=#000000] RegularExpressionObject[/COLOR][COLOR=#000000] = [/COLOR][COLOR=#0000FF]New[/COLOR][COLOR=#000000] [/COLOR][COLOR=#0000FF]RegExp[/COLOR]
                        
                        [COLOR=#000000]    [/COLOR][COLOR=#0000FF]With[/COLOR][COLOR=#000000] RegularExpressionObject[/COLOR]
                        [COLOR=#000000]    [/COLOR][COLOR=#008000]'Pattern to strip out unwanted characters'[/COLOR]
                        [COLOR=#000000]    .Pattern[/COLOR][COLOR=#000000] = [/COLOR][COLOR=#808080]"{lhs: "[/COLOR][COLOR=#808080]"1 U.S. dollar"[/COLOR][COLOR=#808080]",rhs: "[/COLOR][COLOR=#808080]""[/COLOR]
                        [COLOR=#000000]    .IgnoreCase[/COLOR][COLOR=#000000] = [/COLOR][COLOR=#0000FF]True[/COLOR]
                        [COLOR=#000000]    .Global[/COLOR][COLOR=#000000] = [/COLOR][COLOR=#0000FF]True[/COLOR]
                        [COLOR=#000000]    [/COLOR][COLOR=#0000FF]End[/COLOR][COLOR=#000000] [/COLOR][COLOR=#0000FF]With[/COLOR]
                        [COLOR=#000000]    [/COLOR]
                        [COLOR=#000000]    [/COLOR][COLOR=#008000]'Strips out unwanted characters from the begining of the http_req response textand replaces it with an empty string'[/COLOR]
                        [COLOR=#000000]    ReplacedString[/COLOR][COLOR=#000000] = RegularExpressionObject.Replace[/COLOR][COLOR=#000000](http_req.responseText[/COLOR][COLOR=#000000], [/COLOR][COLOR=#808080]""[/COLOR][COLOR=#000000])[/COLOR]
                        [COLOR=#000000]    [/COLOR]
                        [COLOR=#000000]    [/COLOR][COLOR=#008000]'Strips out unwanted characters from the end of the string and replaces it with an empty string'[/COLOR]
                        [COLOR=#000000]    [/COLOR][COLOR=#0000FF]With[/COLOR][COLOR=#000000] RegularExpressionObject[/COLOR]
                        [COLOR=#000000]    [/COLOR][COLOR=#008000]'Pattern to strip out unwanted characters'[/COLOR]
                        [COLOR=#000000]    .Pattern[/COLOR][COLOR=#000000] = [/COLOR][COLOR=#808080]""[/COLOR][COLOR=#808080]",error: "[/COLOR][COLOR=#808080]""[/COLOR][COLOR=#808080]",icc: true}"[/COLOR]
                        [COLOR=#000000]    .IgnoreCase[/COLOR][COLOR=#000000] = [/COLOR][COLOR=#0000FF]True[/COLOR]
                        [COLOR=#000000]    .Global[/COLOR][COLOR=#000000] = [/COLOR][COLOR=#0000FF]True[/COLOR]
                        [COLOR=#000000]    [/COLOR][COLOR=#0000FF]End[/COLOR][COLOR=#000000] [/COLOR][COLOR=#0000FF]With[/COLOR]
                        [COLOR=#000000]    [/COLOR]
                        [COLOR=#000000]    ReplacedString[/COLOR][COLOR=#000000] = RegularExpressionObject.Replace[/COLOR][COLOR=#000000](ReplacedString[/COLOR][COLOR=#000000], [/COLOR][COLOR=#808080]""[/COLOR][COLOR=#000000])[/COLOR]
                        [COLOR=#000000]    [/COLOR]
                        [COLOR=#000000]    [/COLOR][COLOR=#0000FF]Set[/COLOR][COLOR=#000000] oWriteObj[/COLOR][COLOR=#000000] = [/COLOR][COLOR=#0000FF]CreateObject[/COLOR][COLOR=#000000]([/COLOR][COLOR=#808080]"Scripting.FileSystemObject"[/COLOR][COLOR=#000000])[/COLOR]
                        [COLOR=#000000]    [/COLOR][COLOR=#008000]'Creates file if one is not already created inspecified path'[/COLOR]
                        [COLOR=#000000]    [/COLOR][COLOR=#0000FF]Set[/COLOR][COLOR=#000000] oWrite[/COLOR][COLOR=#000000] = oWriteObj.OpenTextFile[/COLOR][COLOR=#000000]([/COLOR][COLOR=#808080]"c:/output.txt"[/COLOR][COLOR=#000000], [/COLOR][COLOR=#800000]8[/COLOR][COLOR=#000000], [/COLOR][COLOR=#0000FF]True[/COLOR][COLOR=#000000])[/COLOR]
                        [COLOR=#000000]    [/COLOR]
                        [COLOR=#000000]    [/COLOR][COLOR=#008000]'Writes value to output file'[/COLOR]
                        [COLOR=#000000]    oWrite.WriteLine[/COLOR][COLOR=#000000](ReplacedString[/COLOR][COLOR=#000000] & [/COLOR][COLOR=#808080]","[/COLOR][COLOR=#000000])[/COLOR]
                        [COLOR=#000000]    oWrite.Close[/COLOR][COLOR=#000000]()[/COLOR]
                        [COLOR=#000000]    [/COLOR]
                        [COLOR=#000000]    [/COLOR][COLOR=#0000FF]Set[/COLOR][COLOR=#000000] oReadObj[/COLOR][COLOR=#000000]  = [/COLOR][COLOR=#0000FF]CreateObject[/COLOR][COLOR=#000000]([/COLOR][COLOR=#808080]"Scripting.FileSystemObject"[/COLOR][COLOR=#000000])[/COLOR]
                        [COLOR=#000000]    [/COLOR][COLOR=#0000FF]Set[/COLOR][COLOR=#000000] oRead[/COLOR][COLOR=#000000] = oReadObj.OpenTextFile[/COLOR][COLOR=#000000]([/COLOR][COLOR=#808080]"c:/output.txt"[/COLOR][COLOR=#000000], [/COLOR][COLOR=#800000]1[/COLOR][COLOR=#000000], [/COLOR][COLOR=#0000FF]True[/COLOR][COLOR=#000000])[/COLOR]
                        [COLOR=#000000]    [/COLOR][COLOR=#0000FF]Set[/COLOR][COLOR=#000000] myArrayList[/COLOR][COLOR=#000000] = [/COLOR][COLOR=#0000FF]CreateObject[/COLOR][COLOR=#000000]([/COLOR][COLOR=#808080]"System.Collections.ArrayList"[/COLOR][COLOR=#000000])[/COLOR]
                        [COLOR=#000000]    [/COLOR]
                        [COLOR=#000000]    [/COLOR][COLOR=#0000FF]Dim[/COLOR][COLOR=#000000] strLine[/COLOR]
                        [COLOR=#000000]    [/COLOR][COLOR=#0000FF]Dim[/COLOR][COLOR=#000000] intLineCounter[/COLOR]
                        [COLOR=#000000]    [/COLOR]
                        [COLOR=#000000]    intLineCounter[/COLOR][COLOR=#000000] = [/COLOR][COLOR=#800000]0[/COLOR]
                        [COLOR=#000000]    [/COLOR]
                        [COLOR=#000000]    [/COLOR][COLOR=#008000]'Iterates through txt file[/COLOR]
                        [COLOR=#000000]    [/COLOR][COLOR=#0000FF]Do[/COLOR][COLOR=#000000] [/COLOR][COLOR=#0000FF]Until[/COLOR][COLOR=#000000] oRead.AtEndOfStream[/COLOR]
                        [COLOR=#000000]    [/COLOR]
                        [COLOR=#000000]    strNextLine[/COLOR][COLOR=#000000] = oRead.ReadLine[/COLOR]
                        [COLOR=#000000]    [/COLOR][COLOR=#008000]'Places values from txt file into array list which are separated by commas'[/COLOR]
                        [COLOR=#000000]    myArrayList[/COLOR][COLOR=#000000] = [/COLOR][COLOR=#0000FF]Split[/COLOR][COLOR=#000000](strNextLine[/COLOR][COLOR=#000000] , [/COLOR][COLOR=#808080]","[/COLOR][COLOR=#000000])[/COLOR]
                        [COLOR=#000000]    [/COLOR][COLOR=#008000]'Increments arraylist,in order toretrieve the latest value from the log file'[/COLOR]
                        [COLOR=#000000]    intLineCounter[/COLOR][COLOR=#000000] = intLineCounter[/COLOR][COLOR=#000000] + [/COLOR][COLOR=#800000]1[/COLOR]
                        [COLOR=#000000]    [/COLOR]
                        [COLOR=#000000]    [/COLOR][COLOR=#0000FF]Loop[/COLOR]
                        [COLOR=#000000]    [/COLOR]
                        [COLOR=#000000]    [/COLOR][COLOR=#008000]'Displays latest value from logfile'[/COLOR]
                        [COLOR=#000000]    WScript.Echo[/COLOR][COLOR=#000000] myArrayList[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]0[/COLOR][COLOR=#000000])[/COLOR]
                        [COLOR=#000000]    [/COLOR]
                        [COLOR=#000000]    oRead.Close[/COLOR][COLOR=#000000]()[/COLOR]
                        
                        [COLOR=#000000]    [/COLOR][COLOR=#0000FF]Set[/COLOR][COLOR=#000000] RegularExpressionObject[/COLOR][COLOR=#000000] = [/COLOR][COLOR=#0000FF]Nothing[/COLOR]
                        [COLOR=#000000]    [/COLOR][COLOR=#0000FF]Set[/COLOR][COLOR=#000000] http_req[/COLOR][COLOR=#000000] = [/COLOR][COLOR=#0000FF]nothing[/COLOR]
                        
                        [/FONT]

                        Comment


                          #72
                          This pretty much meets spec:

                          Code:
                          Set objArgs = WScript.Arguments
                          Dim http_req
                          Set http_req = CreateObject("Msxml2.ServerXMLHTTP.3.0")
                          Set RegularExpressionObject = New RegExp
                          
                          
                          Do 
                              http_req.open "GET", "http://www.google.com/ig/calculator?hl=en&q=1USD=?GBP", False
                              http_req.send
                              
                              ' If not OK then wait before trying again
                              If http_req.status <> 200 Then
                              	' wait 1 second
                                  WScript.Sleep 1000    
                              End If
                          
                          
                              With RegularExpressionObject
                              'Pattern to strip out unwanted characters'
                              .Pattern = "{lhs: ""1 U.S. dollar"",rhs: """
                              .IgnoreCase = True
                              .Global = True
                              End With
                              
                              'Strips out unwanted characters from the begining of the http_req response textand replaces it with an empty string'
                              ReplacedString = RegularExpressionObject.Replace(http_req.responseText, "")
                              
                              'Strips out unwanted characters from the end of the string and replaces it with an empty string'
                              With RegularExpressionObject
                              'Pattern to strip out unwanted characters'
                              .Pattern = """,error: """",icc: true}"
                              .IgnoreCase = True
                              .Global = True
                              End With
                              
                              ReplacedString = RegularExpressionObject.Replace(ReplacedString, "")
                              
                              Set oWriteObj = CreateObject("Scripting.FileSystemObject")
                              'Creates file if one is not already created inspecified path'
                              Set oWrite = oWriteObj.OpenTextFile("c:/output.txt", 8, True)
                              
                              'Writes value to output file'
                              oWrite.WriteLine(ReplacedString & ",")
                              oWrite.Close()
                              
                              Set oReadObj  = CreateObject("Scripting.FileSystemObject")
                              Set oRead = oReadObj.OpenTextFile("c:/output.txt", 1, True)
                              Set myArrayList = CreateObject("System.Collections.ArrayList")
                              
                              Dim strLine
                              Dim intLineCounter
                              
                              intLineCounter = 0
                              
                              'Iterates through txt file
                              Do Until oRead.AtEndOfStream
                              
                              strNextLine = oRead.ReadLine
                              'Places values from txt file into array list which are separated by commas'
                              myArrayList = Split(strNextLine , ",")
                              'Increments arraylist,in order toretrieve the latest value from the log file'
                              intLineCounter = intLineCounter + 1
                              
                              Loop
                              
                              'Displays latest value from logfile'
                              WScript.Echo myArrayList(0)
                          
                          
                              ' Wait 5 minutes before asking again
                              WScript.Sleep(1000 * 60 * 5)
                          Loop While http_req.status = 200
                          
                          
                          oRead.Close()
                          
                          
                          Set RegularExpressionObject = Nothing
                          Set http_req = nothing
                          Some observations:

                          1. Asking to write to the C: drive and expecting to be allowed to do that is a bad idea. Look up how to open a temporary file in the user's private area
                          2. Indentation - Possibly this went wrong when you posted the code but make sure it's good in the real version

                          Good luck mate
                          Last edited by Brad; 21-07-2011, 11:56.

                          Comment


                            #73
                            Big thanks for advice given in the thread, unfortunately I came 2nd place for the job. Guy who did apparently knocked something up in six hours and sent it in the next day, back to square one I go.

                            Comment


                              #74
                              On the plus side, you now won't be using VB all day

                              Comment


                                #75
                                To be fair on VB, modern VB (VB.NET) is nothing like old VB (VB6, VBA, VBScript). IIRC Microsoft are even going to adapt XNA to be programmable using VB.NET.

                                Comment

                                Working...
                                X