Jump to content

General Logic


Iceburg
 Share

Recommended Posts

I have a problem, that I can code, but I can't get the logic in my head on how to code it.

I have a script that does the following:

- Grab all the URL's on a page.

local $Array[1]
        For $oLink In $oLinks
            if StringInStr ($oLink.href, "/flighttimes") Then
                ConsoleWrite($olink.href & @CR)
                _ArrayAdd($Array, $oLink.href)
            EndIf
        Next

I want to compare each of the URL's from the page to the URL's in the array. I know there is an easier way, but the thought I have in my head is something like:

local $j = 0
    local $k = ubound($Array) - 1

    while $j < $k
        ;MsgBox(1, "", "$Array[$j] " & $Array[$j])
        ;_ArrayDisplay($Array)
        if ubound($Array, 0) = 1 then ExitLoop
        if $Array[$j] = "http://url/flight1234" then 
            _ArrayDelete($Array, $j)
            $j -= 1
                elseif $Array[$j] = "http://url/flight1235" then 
            _ArrayDelete($RareArray, $j)
            $j -= 1
.... etc, etc, etc,

Two issues, I know this can be done easier and like I said, I can't get my head on the logic, and even like it is now, it doesn't work cause I end up with URLs that I have already searched for cause the index of the URLs change.

Help a wanna-be coder?

Link to comment
Share on other sites

Help a wanna-be coder?

Why not use _ArrayUnique ?

From AutoIt help:

; *****************************************************************************

; Example 1 - Declare a 1-dimensional array, that contains duplicate values.

; Use _ArrayUnique to Create a New Array that only contains the unique values.

; *****************************************************************************

Link to comment
Share on other sites

What is $RareArray? I assume that it contains links that have been previously viewed, but I am confused why you would ever delete any elements in that array, as you have done in your example.

In general, I prefer one of two ways to cycle through a loop if I am using _ArrayDelete

For $Element In $Array  ;works great with zero-based Arrays, need to make sure to catch the counter in 1 based arrays
or
For $i = UBound($Array) -1 To 0 Step -1  ;Easier to change last value to 1 with 1-based array

It can be done with a counter or increasing incremental counters, but it requires a greater mind than mine to visualize the potential errors in the logic.

In short, if you are potentially deleting a value from either array, it would be easy to use one of the two ways that I described with one loop nested inside the other if you are checking all elements against all other elements.

Link to comment
Share on other sites

Why not use _ArrayUnique ?

From AutoIt help:

; *****************************************************************************

; Example 1 - Declare a 1-dimensional array, that contains duplicate values.

; Use _ArrayUnique to Create a New Array that only contains the unique values.

; *****************************************************************************

I need to compare two array's:

Array1[0]= "http://www.test.com/test1"

Array1[1]= "http://www.test.com/test2"

Array1[2]= "http://www.test.com/test3"

Array2[0] = Array1[0]= "http://www.test.com/test1"

Array2[1] = Array1[0]= "http://www.test.com/test2"

Array2[2] = Array1[0]= "http://www.test.com/test4"

I want to compare all the results from array1 to every entry in array2. I want a final array that only has the URL's from array2, that don't exist in Array1. In this case there would only be one URL remaining and thats http://www.test.com/test4

That more clear? I understand arrayunique to compare each URL in the array to the others, and return only uniques, not two arrays.

Edited by Iceburg
Link to comment
Share on other sites

What is $RareArray? I assume that it contains links that have been previously viewed, but I am confused why you would ever delete any elements in that array, as you have done in your example.

In general, I prefer one of two ways to cycle through a loop if I am using _ArrayDelete

For $Element In $Array  ;works great with zero-based Arrays, need to make sure to catch the counter in 1 based arrays
or
For $i = UBound($Array) -1 To 0 Step -1  ;Easier to change last value to 1 with 1-based array

It can be done with a counter or increasing incremental counters, but it requires a greater mind than mine to visualize the potential errors in the logic.

In short, if you are potentially deleting a value from either array, it would be easy to use one of the two ways that I described with one loop nested inside the other if you are checking all elements against all other elements.

Varian, $rareArray is the name of my array in the script, I cleaned it up to make it easier to read and understand for the sake of the forum, I missed changing that one apparently. What is a 0 based vs a 1 based array?

I don't need to delete anything, I am just trying to end up with a list of URL's that are different. 99% of the time there will be 0 URL's remaining, but that 1% of the time is the URL I want to find and notify myself about. Are you suggesting a third array with the differences?

Link to comment
Share on other sites

Possibly easier to do something like this

#Include<Array.au3>
_ArrayDisplay($aArray1)
$sHold = _ArrayToString($aArray1, "|") & "|"
For $i = 0 To Ubound($aArray2) -1
    If NOT StringInStr($sHold, $aArray2[$i] & "|") Then $sHold &= $aArray2[$i] & "|"
Next
$aArray1 = StringSplit(StringTrimRight($sHold, 1), "|", 2)
_ArrayDisplay($aArray1)

Also you could use _ArrayUnique() as suggested simply by using _ArrayConcatenate() first.

Now, if I read this wrong and you really only want $aArray2() to only contain the elements not already found in $aArray1 (in this case "http://www.test.com/test4") then you can use something similar to what i gave you above.

#Include<Array.au3>
_ArrayDisplay($aArray1)
$sHold = _ArrayToString($aArray2, "|") & "|"
For $i = 0 To Ubound($aArray1) -1
    If StringInStr($sHold, $aArray1[$i] & "|") Then $sHold &= StringReplace($sHold, $aArray1[$i] & "|", "")
Next
;; Do a check here to make sure the last character in $sHold is "|"
$aArray1 = StringSplit(StringTrimRight($sHold, 1), "|", 2)
_ArrayDisplay($aArray1)

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

This is where I am at, this seems to get real close... I'm doing it this way as its possible that there will be no URL's left, and in fact likely that will be the case, so after this is done, I need to pseudo code "if there are URL's, navigate to each one"

;My Array's for testing
;---------------
;
;
$RareArray[0] = "http://www.test.com/1"
$RareArray[1] = "http://www.test.com/2"
$RareArray[2] = "http://www.test.com/3"
$RareArray[3] = "http://www.test.com/4"
$RareArray[4] = "http://www.test.com/5"
$RareArray[5] = "http://www.test.com/6"
$RareArray[6] = "http://www.test.com/7"
$RareArray[7] = "http://www.test.com/8"
$RareArray[8] = "http://www.test.com/9"
$RareArray[9] = "http://www.test.com/10"
$RareArray[10] = "http://www.test.com/11"
$RareArray[11] = "http://www.test.com/12"
$RareArray[12] = "http://www.test.com/13"
$RareArray[13] = "http://www.test.com/14"
$RareArray[14] = "http://www.test.com/15"
$ExistingArray[0] = "http://www.test.com/1"
$ExistingArray[1] = "http://www.test.com/2"
$ExistingArray[2] = "http://www.test.com/3"
$ExistingArray[3] = "http://www.test.com/4"
$ExistingArray[4] = "http://www.test.com/5"
$ExistingArray[5] = "http://www.test.com/6"
$ExistingArray[6] = "http://www.test.com/7"
$ExistingArray[7] = "http://www.test.com/8"
$ExistingArray[8] = "http://www.test.com/9"
$ExistingArray[9] = "http://www.test.com/10"
$ExistingArray[10] = "http://www.test.com/11"
$ExistingArray[11] = "http://www.test.com/12"
$ExistingArray[12] = "http://www.test.com/13"
$ExistingArray[13] = "http://www.test.com/14"
$ExistingArray[14] = "http://www.test.com/15"



    $Max_rarearray = ubound($RareArray)       ; rarearray = 15 for now to start
    $Max_Existingarray = ubound($ExistingArray)   ; existing = 15 URL, each matching rarearray
    For $i=0 To $Max_rarearray step 1               ; start at rarearray0
        while $j < $Max_rarearray   
            ConsoleWrite("$i =" & $i & "$j =" & $j & @CRLF)
            if $ExistingArray[$i] == $RareArray[$j] then    ; start at existingarray0
                _ArrayDelete($RareArray, $j)
                $Max_rarearray -= 1
            EndIf
            $j += 1
        WEnd
        $j = 0
    Next

    _ArrayDisplay($rarearray)
    _ArrayDisplay($existingarray)
Link to comment
Share on other sites

This also doesn't work, I am missing something...

#include <Array.au3>

local $rarearray[16]
local $existingarray[15]
local $i=0
local $j=0

$RareArray[0] = "http://www.test.com/1"
$RareArray[1] = "http://www.test.com/2"
$RareArray[2] = "http://www.test.com/3"
$RareArray[3] = "http://www.test.com/4"
$RareArray[4] = "http://www.test.com/5"
$RareArray[5] = "http://www.test.com/6"
$RareArray[6] = "http://www.test.com/7"
$RareArray[7] = "http://www.test.com/8"
$RareArray[8] = "http://www.test.com/9"
$RareArray[9] = "http://www.test.com/10"
$RareArray[10] = "http://www.test.com/11"
$RareArray[11] = "http://www.test.com/12"
$RareArray[12] = "http://www.test.com/13"
$RareArray[13] = "http://www.test.com/14"
$RareArray[14] = "http://www.test.com/15"
$RareArray[15] = "http://www.test.com/16"
$ExistingArray[0] = "http://www.test.com/1"
$ExistingArray[1] = "http://www.test.com/2"
$ExistingArray[2] = "http://www.test.com/3"
$ExistingArray[3] = "http://www.test.com/4"
$ExistingArray[4] = "http://www.test.com/5"
$ExistingArray[5] = "http://www.test.com/6"
$ExistingArray[6] = "http://www.test.com/7"
$ExistingArray[7] = "http://www.test.com/8"
$ExistingArray[8] = "http://www.test.com/9"
$ExistingArray[9] = "http://www.test.com/10"
$ExistingArray[10] = "http://www.test.com/11"
$ExistingArray[11] = "http://www.test.com/12"
$ExistingArray[12] = "http://www.test.com/13"
$ExistingArray[13] = "http://www.test.com/14"
$ExistingArray[14] = "http://www.test.com/15"

;_ArrayDisplay($rarearray)
local $sHold = _ArrayToString($rarearray, "|") & "|"
;ConsoleWrite($sHold & @CRLF)
For $i = 0 To Ubound($existingarray) -1
    If NOT StringInStr($sHold, $existingarray[$i] & "|") Then $sHold &= $existingarray[$i] & "|"
Next
;ConsoleWrite($sHold & @CRLF)
$rarearray = StringSplit(StringTrimRight($sHold, 1), "|", 2)
ConsoleWrite($rarearray)
;StringSplit(
;_ArrayDisplay($rarearray)
Link to comment
Share on other sites

Try this:

#include <Array.au3>

Local $rarearray[16]
Local $existingarray[15]
Local $i = 0
Local $j = 0

$rarearray[0] = "http://www.test.com/1"
$rarearray[1] = "http://www.test.com/2"
$rarearray[2] = "http://www.test.com/3"
$rarearray[3] = "http://www.test.com/4"
$rarearray[4] = "http://www.test.com/5"
$rarearray[5] = "http://www.test.com/6"
$rarearray[6] = "http://www.test.com/7"
$rarearray[7] = "http://www.test.com/8"
$rarearray[8] = "http://www.test.com/9"
$rarearray[9] = "http://www.test.com/10"
$rarearray[10] = "http://www.test.com/11"
$rarearray[11] = "http://www.test.com/12"
$rarearray[12] = "http://www.test.com/13"
$rarearray[13] = "http://www.test.com/14"
$rarearray[14] = "http://www.test.com/15"
$rarearray[15] = "http://www.test.com/16"
$existingarray[0] = "http://www.test.com/1"
$existingarray[1] = "http://www.test.com/2"
$existingarray[2] = "http://www.test.com/3"
$existingarray[3] = "http://www.test.com/4"
$existingarray[4] = "http://www.test.com/5"
$existingarray[5] = "http://www.test.com/6"
$existingarray[6] = "http://www.test.com/7"
$existingarray[7] = "http://www.test.com/8"
$existingarray[8] = "http://www.test.com/9"
$existingarray[9] = "http://www.test.com/10"
$existingarray[10] = "http://www.test.com/11"
$existingarray[11] = "http://www.test.com/12"
$existingarray[12] = "http://www.test.com/13"
$existingarray[13] = "http://www.test.com/14"
$existingarray[14] = "http://www.test.com/15"

For $i = 0 To UBound($existingarray) - 1
    For $X = UBound($rarearray) - 1 To 0 Step -1
        If $rarearray[$X] = $existingarray[$i] Then
            _ArrayDelete($rarearray, $X)
        EndIf
    Next
Next
_ArrayDisplay($rarearray)
_ArrayDisplay($existingarray)

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Try this:

#include <Array.au3>

Local $rarearray[16]
Local $existingarray[15]
Local $i = 0
Local $j = 0

$rarearray[0] = "http://www.test.com/1"
$rarearray[1] = "http://www.test.com/2"
$rarearray[2] = "http://www.test.com/3"
$rarearray[3] = "http://www.test.com/4"
$rarearray[4] = "http://www.test.com/5"
$rarearray[5] = "http://www.test.com/6"
$rarearray[6] = "http://www.test.com/7"
$rarearray[7] = "http://www.test.com/8"
$rarearray[8] = "http://www.test.com/9"
$rarearray[9] = "http://www.test.com/10"
$rarearray[10] = "http://www.test.com/11"
$rarearray[11] = "http://www.test.com/12"
$rarearray[12] = "http://www.test.com/13"
$rarearray[13] = "http://www.test.com/14"
$rarearray[14] = "http://www.test.com/15"
$rarearray[15] = "http://www.test.com/16"
$existingarray[0] = "http://www.test.com/1"
$existingarray[1] = "http://www.test.com/2"
$existingarray[2] = "http://www.test.com/3"
$existingarray[3] = "http://www.test.com/4"
$existingarray[4] = "http://www.test.com/5"
$existingarray[5] = "http://www.test.com/6"
$existingarray[6] = "http://www.test.com/7"
$existingarray[7] = "http://www.test.com/8"
$existingarray[8] = "http://www.test.com/9"
$existingarray[9] = "http://www.test.com/10"
$existingarray[10] = "http://www.test.com/11"
$existingarray[11] = "http://www.test.com/12"
$existingarray[12] = "http://www.test.com/13"
$existingarray[13] = "http://www.test.com/14"
$existingarray[14] = "http://www.test.com/15"

For $i = 0 To UBound($existingarray) - 1
    For $X = UBound($rarearray) - 1 To 0 Step -1
        If $rarearray[$X] = $existingarray[$i] Then
            _ArrayDelete($rarearray, $X)
        EndIf
    Next
Next
_ArrayDisplay($rarearray)
_ArrayDisplay($existingarray)

You're a genius, thanks!

Link to comment
Share on other sites

#include <array.au3>
#include <IE.au3>

Dim $RareArray[16]
Dim $ExistingArray[16]

;My Array's for testing
;---------------
;
;
$RareArray[0] = "http://www.test.com/1"
$RareArray[15] = "http://www.test.com/2"
$RareArray[2] = "http://www.test.com/3"
$RareArray[3] = "http://www.test.com/4"
$RareArray[4] = "http://www.test.com/5"
$RareArray[5] = "http://www.test.com/6"
$RareArray[6] = "http://www.test.com/7"
$RareArray[7] = "http://www.test.com/8"
$RareArray[8] = "http://www.test.com/9"
$RareArray[9] = "http://www.test.com/10"
$RareArray[10] = "http://www.test.com/11"
$RareArray[11] = "http://www.test.com/12"
$RareArray[12] = "http://www.test.com/13"
$RareArray[13] = "http://www.test.com/14"
$RareArray[14] = "http://www.test.com/15"
$RareArray[1] = "http://www.google.com"
$ExistingArray[0] = "http://www.test.com/1"
$ExistingArray[15] = "http://www.test.com/2"
$ExistingArray[2] = "http://www.test.com/3"
$ExistingArray[3] = "http://www.test.com/4"
$ExistingArray[4] = "http://www.test.com/5"
$ExistingArray[5] = "http://www.test.com/6"
$ExistingArray[6] = "http://www.test.com/7"
$ExistingArray[7] = "http://www.test.com/8"
$ExistingArray[8] = "http://www.test.com/9"
$ExistingArray[9] = "http://www.test.com/10"
$ExistingArray[10] = "http://www.test.com/11"
$ExistingArray[11] = "http://www.test.com/12"
$ExistingArray[12] = "http://www.test.com/13"
$ExistingArray[13] = "http://www.test.com/14"
$ExistingArray[14] = "http://www.test.com/15"
$ExistingArray[1] = "http://www.autoitscript.com"

_ArrayConcatenate ($RareArray , $ExistingArray)


for $i = 0 to ubound($RareArray) - 1
    If $RareArray = "" Then exitloop
    $indexArray = ""
    $indexArray = _ArrayFindAll ($RareArray , $RareArray[$i])

If ubound($indexArray) > 1 Then
    for $k = 0 to ubound($indexArray) - 1
        _ArrayDelete ($RareArray , $indexArray[$k] - $k)
    Next
EndIf

If $i >= ubound($RareArray) - 1 Then $i = 0

    $Unique = _ArrayUnique($RareArray)

If ubound($Unique) - 1 = ubound($RareArray) Then ExitLoop

    Next


If isarray($RareArray) Then
for $u = 0 to ubound ($RareArray) - 1
    $oIE = _IECreate ()
_IENavigate ($oIE , $RareArray[$u])
next
Else
    msgbox (0, '' , 'no matches found')
Endif

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

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