Jump to content

array to string question


Recommended Posts

Starting with the following code (which works, and gives a lengthy array),

CODE
#include <array.au3>

#include <inet.au3>

#include <string.au3>

$sURL = "http://www.amazon.com/s/ref=nb_ss_b/102-5993488-0177751?url=search-alias%3Dstripbooks&field-keywords=mystery&Go.x=0&Go.y=0&Go=Go"

Dim $sStart = 'id="Td:0">', $sEnd = '<td width="100%" class="searchitem" id="Td:1">'

$aArray = _StringBetween(_INetGetSource($sURL), $sStart, $sEnd)

If IsArray($aArray) Then _ArrayDisplay($aArray, 'Found:')

I'm trying to add to it so that it pulls just the title information out of that array. Here's my attempt

CODE
#include <array.au3>

#include <inet.au3>

#include <string.au3>

$sURL = "http://www.amazon.com/s/ref=nb_ss_b/102-5993488-0177751?url=search-alias%3Dstripbooks&field-keywords=mystery&Go.x=0&Go.y=0&Go=Go"

Dim $sStart = 'id="Td:0">', $sEnd = '<td width="100%" class="searchitem" id="Td:1">'

$aArray = _StringBetween(_INetGetSource($sURL), $sStart, $sEnd)

If IsArray($aArray) Then _ArrayDisplay($aArray, 'Found:')

Dim $sStart = 'id="Td:0">', $sEnd = '<td width="100%" class="searchitem" id="Td:1">'

$aArray = _StringBetween(_INetGetSource($sURL), $sStart, $sEnd)

Dim $sStart2 = '<span class="srTitle">', $sEnd2 = '</span></a>'

$aArray2 = _StringBetween($aArray, $sStart2, $sEnd2)

If IsArray($aArray2) Then _ArrayDisplay($aArray2, 'title:')

Instead of the title, I get the complete array ($aArray). Do I need to use the _ArrayToString fuction first? This was my guess but, I don't understand what I need to use as a delimiter.

Link to comment
Share on other sites

  • Moderators

All that information, is not a "complete array", all that information is the string.

You are only returning 1 element in that array which is stored in [0] from that. That is your string ([0] is).

So, $aArray[0] holds your value, that's why I asked last night if you wanted "all that" information.

Example:

#include <inet.au3>
#include <string.au3>
$sURL = "http://www.amazon.com/s/ref=nb_ss_b/102-5993488-0177751?url=search-alias%3Dstripbooks&field-keywords=mystery&Go.x=0&Go.y=0&Go=Go"
Dim $sStart = 'id="Td:0">', $sEnd = '<td width="100%" class="searchitem" id="Td:1">'
$aArray = _StringBetween(_INetGetSource($sURL), $sStart, $sEnd)
If IsArray($aArray) Then MsgBox(64, 'Info', $aArray[0])
;Note I took _ArrayDisplay() out, and I'm only showing the first element [0]. Your result should be the same as if you used _ArrayDisplay(). Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

Maybe this is what you are going for?

#include <inet.au3>
#include <string.au3>
$sURL = "http://www.amazon.com/s/ref=nb_ss_b/102-5993488-0177751?url=search-alias%3Dstripbooks&field-keywords=mystery&Go.x=0&Go.y=0&Go=Go"
Dim $sStart = 'id="Td:0">', $sEnd = '<td width="100%" class="searchitem" id="Td:1">'
$sString = _INetGetSource($sURL)
$aArray = _StringBetween($sString, $sStart, $sEnd)
If IsArray($aArray) Then
    $aArray = _StringBetween($aArray[0], 'alt="', '(')
    If IsArray($aArray) Then MsgBox(64, 'Book Title', $aArray[0])
EndIf

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Maybe this is what you are going for?

#include <inet.au3>
#include <string.au3>
$sURL = "http://www.amazon.com/s/ref=nb_ss_b/102-5993488-0177751?url=search-alias%3Dstripbooks&field-keywords=mystery&Go.x=0&Go.y=0&Go=Go"
Dim $sStart = 'id="Td:0">', $sEnd = '<td width="100%" class="searchitem" id="Td:1">'
$sString = _INetGetSource($sURL)
$aArray = _StringBetween($sString, $sStart, $sEnd)
If IsArray($aArray) Then
    $aArray = _StringBetween($aArray[0], 'alt="', '(')
    If IsArray($aArray) Then MsgBox(64, 'Book Title', $aArray[0])
EndIf
Yes, thanks again for your help. That is the information I am looking for. I'm very confused about how you got it and Arrays in general but maybe a few hours of scratching my head will bring some light to it.

Thanks again.

Link to comment
Share on other sites

Maybe this is what you are going for?

#include <inet.au3>
#include <string.au3>
$sURL = "http://www.amazon.com/s/ref=nb_ss_b/102-5993488-0177751?url=search-alias%3Dstripbooks&field-keywords=mystery&Go.x=0&Go.y=0&Go=Go"
Dim $sStart = 'id="Td:0">', $sEnd = '<td width="100%" class="searchitem" id="Td:1">'
$sString = _INetGetSource($sURL)
$aArray = _StringBetween($sString, $sStart, $sEnd)
If IsArray($aArray) Then
    $aArray = _StringBetween($aArray[0], 'alt="', '(')
    If IsArray($aArray) Then MsgBox(64, 'Book Title', $aArray[0])
EndIf
I'm trying to understand this. Does the following line:

"$aArray = _StringBetween($sString, $sStart, $sEnd)"

cause the string to be saved as $aArray[0] by default? Why can't I just store it as a variant? When I try I don't get the string.

Is this line:

"If IsArray($aArray) Then"

checking to see if the value in the array is an array? That's my understanding from reading the help file but just wanted to make sure.

In the following line:

"$aArray = _StringBetween($aArray[0], 'alt="', '(')"

does this overwrite the original larger string? If so, how do I save it so I can extract other strings from it? If not, how to I "go back" do extract another "_StringBetween" from it (the original large string)?

Link to comment
Share on other sites

  • Moderators

1. What's wrong with just using $aArray[0]? That's how it is returned, unless you do: $aArray = $aArray[0] destroying the Array, or create another variable, you'll have to use it as an Array with [0], don't fight it, just go with it.

2. Yes, that's checking to make sure that we found our string we were looking for, if it isn't an array, there is no sense in going on because $aArray doesn't contain any value to do our next search function.

3. $sString stores the Original string retrieved from _InetGetSource().

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

1. What's wrong with just using $aArray[0]? That's how it is returned, unless you do: $aArray = $aArray[0] destroying the Array, or create another variable, you'll have to use it as an Array with [0], don't fight it, just go with it.

2. Yes, that's checking to make sure that we found our string we were looking for, if it isn't an array, there is no sense in going on because $aArray doesn't contain any value to do our next search function.

3. $sString stores the Original string retrieved from _InetGetSource().

Thanks, things are starting to get a little clearer. I still have a question about #3. I'm trying to understand where the string between is stored (not the string which includes the entire page but the string which the title is extracted from). It's initially stored as $aArray[0] but then I believe it is overwritten by the "title" string which is then displayed. I'm wondering if there's a way to keep this string so I can extract other strings from it in addition to the title, without having to go back and pull the large string out of the page again.

Hope this makes sense.

Link to comment
Share on other sites

  • Moderators

Thanks, things are starting to get a little clearer. I still have a question about #3. I'm trying to understand where the string between is stored (not the string which includes the entire page but the string which the title is extracted from). It's initially stored as $aArray[0] but then I believe it is overwritten by the "title" string which is then displayed. I'm wondering if there's a way to keep this string so I can extract other strings from it in addition to the title, without having to go back and pull the large string out of the page again.

Hope this makes sense.

Sure, just do (pseudo code):

$sString = _InetGetSource
$sSecondary = ''
$aArray = _StringBetween()
If IsArray($aArray) Then
$sSecondary = $aArray[0]
$aArray = _StringBetween($sSecondary)
Now $sSecondary will have the original in-between string stored.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Sure, just do (pseudo code):

$sString = _InetGetSource
$sSecondary = ''
$aArray = _StringBetween()
If IsArray($aArray) Then
$sSecondary = $aArray[0]
$aArray = _StringBetween($sSecondary)
Now $sSecondary will have the original in-between string stored.
Thanks, I just figured out a way by storing the string as a variable then setting the string back to that variable as follows.... (looks like your sample might be a cleaner solution though...

thanks again.

CODE
#include <array.au3>

#include <inet.au3>

#include <string.au3>

$sURL = "http://www.amazon.com/s/ref=nb_ss_b/102-5993488-0177751?url=search-alias%3Dstripbooks&field-keywords=mystery&Go.x=0&Go.y=0&Go=Go"

Dim $sStart = 'id="Td:0">', $sEnd = '<td width="100%" class="searchitem" id="Td:1">'

$sString = _INetGetSource($sURL)

$aArray = _StringBetween($sString, $sStart, $sEnd)

$aVar = $aArray[0]

If IsArray($aArray) Then

$aArray = _StringBetween($aArray[0], 'alt="', '(')

If IsArray($aArray) Then MsgBox(64, 'Book Title', $aArray[0])

EndIf

$aArray[0] = $aVar

If IsArray($aArray) Then

$aArray = _StringBetween($aArray[0], 'otherprice">$', '</span>')

If IsArray($aArray) Then MsgBox(64, 'other price', $aArray[0])

EndIf

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...