Parsing a string into an array?

D

Dave F.

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.
 
T

Tim Williams

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
 
N

NickHK

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
 
R

Ron Rosenfeld

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
 
R

Ron Rosenfeld

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top