Sign in to follow this  
Followers 0
doomkiller

Cutting a line totally out of an array

10 posts in this topic

#1 ·  Posted (edited)

Ok here is my array

dim $array[12]
$array[0]  = "Active Connections"
$array[1]  = ""
$array[2]  = "  Proto  Local Address          Foreign Address       State"
$array[3]  = ""
$array[4]  = "  TCP 192.168.0.101:1171   64.12.24.196:5190    ESTABLISHED"
$array[5]  = "  TCP 192.168.0.101:1306   207.150.170.236:6667   ESTABLISHED"
$array[6]  = "  TCP 192.168.0.101:1688   63.241.83.11:6112    TIME_WAIT"
$array[7]  = "  TCP 192.168.0.101:1689   63.241.83.8:6112      ESTABLISHED"
$array[8]  = "  TCP 192.168.0.101:1690   63.241.83.18:6112    TIME_WAIT"
$array[9]  = "  TCP 192.168.0.101:1692   63.241.83.9:6112      TIME_WAIT"
$array[10] = "  TCP 192.168.0.101:1693   63.241.83.9:6112      TIME_WAIT"
$array[11] = "  TCP 192.168.0.101:1694   63.241.83.9:6112      TIME_WAIT"

what i need to do is cut any line with the local port "1171" totally out of the array. I took a look at stringtrim left & right but i'm not sure how to get them to do what i want. Anyone have any ideas?

i'd also like to add in but i don't think this would matter. This is how i actually get my arrays in my script

Dim $array
FileToArray("C:\ip.txt", $array)

it would look exactley like above though

Edited by doomkiller

Share this post


Link to post
Share on other sites



Does the _ArrayDelete() function (#include <Array.au3>) do what you want?

Share this post


Link to post
Share on other sites

Actually yes it does. I did not notice those array functions on the beta. Thankyou!

Share this post


Link to post
Share on other sites

Err looks like it didn't do exactley what i wanted. Here is what i have now

#include <Array.au3>
dim $array[12]
$array[0] = "Active Connections"
$array[1] = ""
$array[2] = " Proto Local Address Foreign Address State"
$array[3] = ""
$array[4] = " TCP 192.168.0.101:1171 64.12.24.196:5190 ESTABLISHED"
$array[5] = " TCP 192.168.0.101:1306 207.150.170.236:6667 ESTABLISHED"
$array[6] = " TCP 192.168.0.101:1688 63.241.83.11:6112 TIME_WAIT"
$array[7] = " TCP 192.168.0.101:1689 63.241.83.8:6112 ESTABLISHED"
$array[8] = " TCP 192.168.0.101:1690 63.241.83.18:6112 TIME_WAIT"
$array[9] = " TCP 192.168.0.101:1692 63.241.83.9:6112 TIME_WAIT"
$array[10] = " TCP 192.168.0.101:1693 63.241.83.9:6112 TIME_WAIT"
$array[11] = " TCP 192.168.0.101:1694 63.241.83.9:6112 TIME_WAIT"


$Pos=_ArraySearch($Array, "1171")
_ArrayDisplay( $Array, "Whole array" )
_ArrayDelete( $Array,$Pos)
_ArrayDisplay( $Array, "Removed entry 8" )

unfortuantely i would need the whole line in _ArraySearch and not just 1171 (had to at least try). Is there any way i can have it do just that look at the whole thing and when it sees 1171 in any line, capture that line and then delete it?

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

unfortuantely i would need the whole line in _ArraySearch and not just 1171 (had to at least try). Is there any way i can have it do just that look at the whole thing and when it sees 1171 in any line, capture that line and then delete it?

I think you could loop through the array: (not quite pseudocode): (for $x = 1 to ubound(array))... stringinstr array[$x], 1171...

Then use _arraydelete to delete array element $x...

Look up these functions in the help file, then try the math and post what you have for more feedback...

Edited by jefhal

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format

Share this post


Link to post
Share on other sites

Well i got a little farther. Here is what i have

#include <Array.au3>
dim $array[12]
$array[0] = "Active Connections"
$array[1] = ""
$array[2] = " Proto Local Address Foreign Address State"
$array[3] = ""
$array[4] = " TCP 192.168.0.101:1171 64.12.24.196:5190 ESTABLISHED"
$array[5] = " TCP 192.168.0.101:1306 207.150.170.236:6667 ESTABLISHED"
$array[6] = " TCP 192.168.0.101:1688 63.241.83.11:6112 TIME_WAIT"
$array[7] = " TCP 192.168.0.101:1689 63.241.83.8:6112 ESTABLISHED"
$array[8] = " TCP 192.168.0.101:1690 63.241.83.18:6112 TIME_WAIT"
$array[9] = " TCP 192.168.0.101:1692 63.241.83.9:6112 TIME_WAIT"
$array[10] = " TCP 192.168.0.101:1693 63.241.83.9:6112 TIME_WAIT"
$array[11] = " TCP 192.168.0.101:1694 63.241.83.9:6112 TIME_WAIT"
_ArrayDisplay( $Array, "Whole array" )

for $x = 1 to Ubound($array)-1
If StringInStr ($array[$x], "1171") Then
   _ArrayDelete( $array, $x)
_ArrayDisplay( $Array, "Removed entry" )
      EndIf

Next

Works great except the little error it throws up after it finishes the if statement. I'm not really sure what i'm doing wrong but i'm guessing i'm stopping the loop before it should or something. Anyone know whats wrong?

Share this post


Link to post
Share on other sites

Works great except the little error it throws up after it finishes the if statement. I'm not really sure what i'm doing wrong but i'm guessing i'm stopping the loop before it should or something. Anyone know whats wrong?

Because the length of the array gets shorter. Ubound changes but the for/next loop doesn't reload it with every loop.

Try this code, it may help you.

$y = UBound($array)
$x = 1
Do
    If StringInStr($array[$x], "1171") Then
        $y -= 1
        _ArrayDelete($array, $x)
        _ArrayDisplay($array, "Removed entry")
    EndIf
    $x += 1
Until $x >= $y

Share this post


Link to post
Share on other sites

And another way to do it is to traverse the array backwards:

for $x = (uBound($array - 1) - 1) to 0 step -1
  ...
  next

This ensures that every element in the array is always checked, which I don't think has been covered in the above snippets. For instance, if you have two consecutive entries in the array to be deleted, you would delete the first one (which shifts the remaining elements up by an index) but if you unconditionally increment $x, you will be failing to check the element that was at the new value of $x (it's at the old value which is thought to have been processed) and so the element is skipped, resulting in an uneffective deletion.

I fear I'm not the best at explaining things sometimes, but at least I know what I mean. ;)

Share this post


Link to post
Share on other sites

And another way to do it is to traverse the array backwards:

for $x = (uBound($array - 1) - 1) to 0 step -1
  ...
  next

This ensures that every element in the array is always checked, which I don't think has been covered in the above snippets. For instance, if you have two consecutive entries in the array to be deleted, you would delete the first one (which shifts the remaining elements up by an index) but if you unconditionally increment $x, you will be failing to check the element that was at the new value of $x (it's at the old value which is thought to have been processed) and so the element is skipped, resulting in an uneffective deletion.

I fear I'm not the best at explaining things sometimes, but at least I know what I mean. ;)

You are right. I should have done it like this

If StringInStr($array[$x], "1171") Then
            $y -= 1
            _ArrayDelete($array, $x)
            _ArrayDisplay($array, "Removed entry")
        Else
            $x += 1
        EndIf

But your code is better, easyer and more effective.

Share this post


Link to post
Share on other sites

Ok I see what I did wrong. Its running perfectly now, thankyou for the help guys!

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