![]() |
Parsing a string into an array?
Hi
I'm using HTTPRequest to download a page of stock prices from ADVFN. The part I want is of the format: prices[1]=25.25;prices[2]=2.15;prices[3]=100.5; etc... As you can see it's already passing it to an array within the page functions, but is downloaded as a string. Any ideas how can I parse this info into an array in VBA? I can split using the semi colon but not sure how to get the rest into the array. On a more general note, Does anyone know if there's a better way to obtain parts of a web page data without downloading it all & have to strip out the bits I want? Thanks in advance Dave F. |
Parsing a string into an array?
split() on the semicolon and then loop through the array and use split (on
"=") on each value. Take the second value from that operation and put it in an array (or use it directly). There's no way to donload "part" of a page (at least with respect to the main HTML content: linked content such as images etc are "extra") Tim "Dave F." wrote in message ... Hi I'm using HTTPRequest to download a page of stock prices from ADVFN. The part I want is of the format: prices[1]=25.25;prices[2]=2.15;prices[3]=100.5; etc... As you can see it's already passing it to an array within the page functions, but is downloaded as a string. Any ideas how can I parse this info into an array in VBA? I can split using the semi colon but not sure how to get the rest into the array. On a more general note, Does anyone know if there's a better way to obtain parts of a web page data without downloading it all & have to strip out the bits I want? Thanks in advance Dave F. |
Parsing a string into an array?
Dave,
Depending on the structure/nature of the page, a web query may work. Otherwise you have to download the page and parse the HTML. Depending on what you are doing: Private Sub CommandButton1_Click() Dim Temp As Variant Const HTML As String = "prices[1]=25.25;prices[2]=2.15;prices[3]=100.5" For Each Temp In Split(HTML, ";") MsgBox Split(Temp, "=")(1) Next End Sub NickHK "Dave F." wrote in message ... Hi I'm using HTTPRequest to download a page of stock prices from ADVFN. The part I want is of the format: prices[1]=25.25;prices[2]=2.15;prices[3]=100.5; etc... As you can see it's already passing it to an array within the page functions, but is downloaded as a string. Any ideas how can I parse this info into an array in VBA? I can split using the semi colon but not sure how to get the rest into the array. On a more general note, Does anyone know if there's a better way to obtain parts of a web page data without downloading it all & have to strip out the bits I want? Thanks in advance Dave F. |
Parsing a string into an array?
On Mon, 30 Apr 2007 00:18:47 +0100, "Dave F." wrote:
Hi I'm using HTTPRequest to download a page of stock prices from ADVFN. The part I want is of the format: prices[1]=25.25;prices[2]=2.15;prices[3]=100.5; etc... As you can see it's already passing it to an array within the page functions, but is downloaded as a string. Any ideas how can I parse this info into an array in VBA? I can split using the semi colon but not sure how to get the rest into the array. On a more general note, Does anyone know if there's a better way to obtain parts of a web page data without downloading it all & have to strip out the bits I want? Thanks in advance Dave F. Here's one idea. First replace the "prices...=" part of the string leaving a semicolon delimited string of prices. This uses Regular Expressions. If the line contains other data than what you have shown, the regex will need modification. But it shouldn't be too difficult. Then Split based on the semi-colon. ================================================== = Option Explicit Public Const sPrices As String = "prices[1]=25.25;prices[2]=2.15;prices[3]=100.5" Sub ParsePrices() Dim Prices As Variant Dim objRegExp As Object Dim objMatches As Object Dim i As Long Set objRegExp = CreateObject("VBScript.RegExp") objRegExp.IgnoreCase = True objRegExp.Global = True objRegExp.MultiLine = True objRegExp.Pattern = "prices\[\d+\]=" Prices = objRegExp.Replace(sPrices, "") Prices = Split(Prices, ";") 'Show parsed items For i = 0 To UBound(Prices) Debug.Print i, Prices(i) Next i End Sub ========================================== --ron |
Parsing a string into an array?
On Mon, 30 Apr 2007 06:39:17 -0400, Ron Rosenfeld
wrote: On Mon, 30 Apr 2007 00:18:47 +0100, "Dave F." wrote: Hi I'm using HTTPRequest to download a page of stock prices from ADVFN. The part I want is of the format: prices[1]=25.25;prices[2]=2.15;prices[3]=100.5; etc... As you can see it's already passing it to an array within the page functions, but is downloaded as a string. Any ideas how can I parse this info into an array in VBA? I can split using the semi colon but not sure how to get the rest into the array. On a more general note, Does anyone know if there's a better way to obtain parts of a web page data without downloading it all & have to strip out the bits I want? Thanks in advance Dave F. Here's one idea. First replace the "prices...=" part of the string leaving a semicolon delimited string of prices. This uses Regular Expressions. If the line contains other data than what you have shown, the regex will need modification. But it shouldn't be too difficult. Then Split based on the semi-colon. ================================================= == Option Explicit Public Const sPrices As String = "prices[1]=25.25;prices[2]=2.15;prices[3]=100.5" Sub ParsePrices() Dim Prices As Variant Dim objRegExp As Object Dim objMatches As Object Dim i As Long Set objRegExp = CreateObject("VBScript.RegExp") objRegExp.IgnoreCase = True objRegExp.Global = True objRegExp.MultiLine = True objRegExp.Pattern = "prices\[\d+\]=" Prices = objRegExp.Replace(sPrices, "") Prices = Split(Prices, ";") 'Show parsed items For i = 0 To UBound(Prices) Debug.Print i, Prices(i) Next i End Sub ========================================== --ron There is an unnecessary Dim line in the above. ================================================== = Option Explicit Public Const sPrices As String = "prices[1]=25.25;prices[2]=2.15;prices[3]=100.5" Sub ParsePrices() Dim Prices As Variant Dim objRegExp As Object Dim i As Long Set objRegExp = CreateObject("VBScript.RegExp") objRegExp.IgnoreCase = True objRegExp.Global = True objRegExp.MultiLine = True objRegExp.Pattern = "prices\[\d+\]=" Prices = objRegExp.Replace(sPrices, "") Prices = Split(Prices, ";") 'Show parsed items For i = 0 To UBound(Prices) Debug.Print i, Prices(i) Next i End Sub ========================================== --ron |
All times are GMT +1. The time now is 01:22 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com