Sign in to follow this  
Followers 0
cherdeg

Weird problem...

7 posts in this topic

#1 ·  Posted (edited)

#include <Array.au3>

Local $a_AllFoundComputers[4]   = ["host-00", "host-01", "host-02", "host-03"]
Local $a_TmpFoundComputers  = $a_AllFoundComputers
_ArrayDisplay($a_AllFoundComputers)

For $i=0 To UBound($a_AllFoundComputers) - 1
    $i_Roundtrip = Ping($a_AllFoundComputers[$i], 20)
    If $i_Roundtrip = 0 And @error >= 0 Then 
        _ArrayDelete($a_TmpFoundComputers, $i-1)
    EndIf
Next

_ArrayDisplay($a_TmpFoundComputers)

I'm getting mad!!! Please help me out...given: "host-02" is reachable and online. Normally, I would use the following line to sort out offline hosts from my array: _ArrayDelete($a_TmpFoundComputers, $i). But that doesn't work! For what reason ever "host-01" is the one remaining in "$a_TmpFoundComputers". To get the correct result I have to subtract 1 from "$i". Why???

I'm going crazy here...!

Edited by cherdeg

Share this post


Link to post
Share on other sites



#include <Array.au3>

Local $a_AllFoundComputers[4]   = ["host-00", "host-01", "host-02", "host-03"]
Local $a_TmpFoundComputers  = $a_AllFoundComputers
_ArrayDisplay($a_AllFoundComputers)

For $i=0 To UBound($a_AllFoundComputers) - 1
    $i_Roundtrip = Ping($a_AllFoundComputers[$i], 20)
    If $i_Roundtrip = 0 And @error >= 0 Then 
        _ArrayDelete($a_TmpFoundComputers, $i-1)
    EndIf
Next

_ArrayDisplay($a_TmpFoundComputers)

I'm getting mad!!! Please help me out...given: "host-02" is reachable and online. Normally, I would use the following line to sort out offline hosts from my array: _ArrayDelete($a_TmpFoundComputers, $i). But that doesn't work! For what reason ever "host-01" is the one remaining in "$a_TmpFoundComputers". To get the correct result I have to subtract 1 from "$i". Why???

I'm going crazy here...!

You have not really thought about what happens when you remove an element form the array.

The problem is that you are counting from 0 to Ubound - 1. You should instead count from Ubound -1 down to 0.

The way you are doing it means that when an element is deleted, all the other elements above are shifted down one position. So isay you delete [2] and then delete[3] well you are actually deleting [2] then what was [4]

Try this

#include <Array.au3>

Local $a_AllFoundComputers[4]   = ["host-00", "host-01", "host-02", "host-03"]
Local $a_TmpFoundComputers  = $a_AllFoundComputers
_ArrayDisplay($a_AllFoundComputers)

For $i= UBound($a_AllFoundComputers) - 1 to 0 step -1
    $i_Roundtrip = Ping($a_AllFoundComputers[$i], 20)
    If $i_Roundtrip = 0 And @error >= 0 Then 
        _ArrayDelete($a_TmpFoundComputers, $i-1)
    EndIf
Next

_ArrayDisplay($a_TmpFoundComputers)

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

#include <Array.au3>

Local $a_AllFoundComputers[4]   = ["host-00", "host-01", "host-02", "host-03"]
Local $a_TmpFoundComputers  = $a_AllFoundComputers
_ArrayDisplay($a_AllFoundComputers)

For $i=0 To UBound($a_AllFoundComputers) - 1
    $i_Roundtrip = Ping($a_AllFoundComputers[$i], 20)
    If $i_Roundtrip = 0 And @error >= 0 Then 
        _ArrayDelete($a_TmpFoundComputers, $i-1)
    EndIf
Next

_ArrayDisplay($a_TmpFoundComputers)

I'm getting mad!!! Please help me out...given: "host-02" is reachable and online. Normally, I would use the following line to sort out offline hosts from my array: _ArrayDelete($a_TmpFoundComputers, $i). But that doesn't work! For what reason ever "host-01" is the one remaining in "$a_TmpFoundComputers". To get the correct result I have to subtract 1 from "$i". Why???

I'm going crazy here...!

Hi there m8,

I tryed your script and is going well.

I changed the hostnames, 2 will ping and the other two dont, i all's good.

#include <Array.au3>

Local $a_AllFoundComputers[4]   = ["127.0.0.1", "localhost", "thor", "mastadonte"]
Local $a_TmpFoundComputers  = $a_AllFoundComputers
_ArrayDisplay($a_AllFoundComputers)

For $i=0 To UBound($a_AllFoundComputers) - 1
    $i_Roundtrip = Ping($a_AllFoundComputers[$i], 20)
    If $i_Roundtrip = 0 And @error >= 0 Then 
        ConsoleWrite("Response : " & $i_Roundtrip & " for : " & $a_AllFoundComputers[$i]& @CRLF & "Error : " & @error & @CRLF)
        _ArrayDelete($a_TmpFoundComputers, $i)
    Else
        ConsoleWrite("Response : " & $i_Roundtrip & " for : " & $a_AllFoundComputers[$i]& @CRLF & "Error : " & @error & @CRLF)
    EndIf
Next
_ArrayDisplay($a_AllFoundComputers)
_ArrayDisplay($a_TmpFoundComputers)

Cheers


Old Scriptology

Visual Ping 1.8 - Mass Ping Program with export to txt delimited.

Desktop 2 RGB and YMCK - Pick a color in the desktop and get the RGB and YMCK code.

Desktop 2 RGB - Pick a color in the desktop and get the RGB code.

ShootIT 1.0 - Screen Capture full and partial screen

[font="'Arial Black';"]Remember Remember The Fifth of November.[/font]

Share this post


Link to post
Share on other sites

You have not really thought about what happens when you remove an element form the array.

Hi Martin,

Yes, you're right. My fault. I thought, copying the array would help me solving this problem - but of course the same happens to the temp-array as well. I'll give your solution a shot and report back soon.

Regards, Chris

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Hi there m8,

I tryed your script and is going well.

I changed the hostnames, 2 will ping and the other two dont, i all's good.

Nope, it isn't. Your first two entries reply, the other ones don't, I suppose? Just move the 127.0.0.1 to the third or fourth position and you will see the problem...

(but I'm very glad to see that I'm not the only one having missed Martin's advice) :)

Edited by cherdeg

Share this post


Link to post
Share on other sites

Nope, it isn't. Your first two entries reply, the other ones don't, I suppose? Just move the 127.0.0.1 to the third or fourth position and you will see the problem...

(but I'm very glad to see that I'm not the only one having missed Martin's advice) :)

I see... :)

Well it worked... badly but it did o:)

Cheers and i need to keep my eyes open ;)


Old Scriptology

Visual Ping 1.8 - Mass Ping Program with export to txt delimited.

Desktop 2 RGB and YMCK - Pick a color in the desktop and get the RGB and YMCK code.

Desktop 2 RGB - Pick a color in the desktop and get the RGB code.

ShootIT 1.0 - Screen Capture full and partial screen

[font="'Arial Black';"]Remember Remember The Fifth of November.[/font]

Share this post


Link to post
Share on other sites

I see... :)

Well it worked... badly but it did :)

Cheers and i need to keep my eyes open o:)

...and so do I. I'm glad there was a rational solution for my problem, or I'd definitely gone mad...I was scratching my head for about 3 hours about this...but if you visualize it, it's completely clear: a "stack" of items that is checked from top to bottom.

Martin: thank you so much - your the guy that made my day!

Share this post


Link to post
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
Sign in to follow this  
Followers 0