Jump to content

Filter an Array


 Share

Recommended Posts

Try this:

#include <file.au3>
#include <array.au3>
#include <Date.au3>

$dirpath = "D:\Datatrack\";set the path for the folder where the data extraction files are
$sfilter = "*.PRO"; set the filter for the files you want to extract data from
$FileList = _FileListToArray($dirpath, $sfilter, 1);get the list of files you want to extract data from
If @Error=1 Then
    MsgBox (0,"","No Files\Folders Found.")
    Exit
EndIf
$MA = 1
While $MA <= $FileList[0]
    $fd = StringLeft($FileList[$MA],8)
    if $fd < 20070802 Or $fd > 20070803 Then
        _ArrayDelete ($FileList, $MA)
        $FileList[0]-=1
        ContinueLoop
    Else
        $MA+=1
    EndIf
WEnd
_ArrayDisplay ($FileList)
Edited by danielkza
Link to comment
Share on other sites

To loop through arrays while changing their size you must use While...

Not true.

Going through an array whilst deleting elements using a for loop is easy - I use it all the time -

...but you MUST go from back to front using -1 steps....

for $i = (UBound($array) - 1) to 1 step -1

Also, in my opinion, you're asking for trouble assuming dates are integers. Might not be too much of a problem in this case, but AutoIT has some amazing DATE functions that account for inconsistencies with our 4th dimension!! :)

Just my opinions, no critisism intended :)

- Table UDF - create simple data tables - Line Graph UDF GDI+ - quickly create simple line graphs with x and y axes (uses GDI+ with double buffer) - Line Graph UDF - quickly create simple line graphs with x and y axes (uses AI native graphic control) - Barcode Generator Code 128 B C - Create the 1/0 code for barcodes. - WebCam as BarCode Reader - use your webcam to read barcodes - Stereograms!!! - make your own stereograms in AutoIT - Ziggurat Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Box-Muller Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Elastic Radio Buttons - faux-gravity effects in AutoIT (from javascript)- Morse Code Generator - Generate morse code by tapping your spacebar!
Link to comment
Share on other sites

Oops missed an EndIf in my example above...

for $i = (UBound($array1) - 1) to 1 step -1
      for $j = 1 to (UBound($array2) - 1)
              for $k = 1 to (UBound($array3) - 1)

                    if $array1[$i] >10 And $array2[$j] <4 And $array3[$k] >100 then ; etc etc etc
                     ;<STATEMENT>
                    EndIf
              next
       next
next
- Table UDF - create simple data tables - Line Graph UDF GDI+ - quickly create simple line graphs with x and y axes (uses GDI+ with double buffer) - Line Graph UDF - quickly create simple line graphs with x and y axes (uses AI native graphic control) - Barcode Generator Code 128 B C - Create the 1/0 code for barcodes. - WebCam as BarCode Reader - use your webcam to read barcodes - Stereograms!!! - make your own stereograms in AutoIT - Ziggurat Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Box-Muller Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Elastic Radio Buttons - faux-gravity effects in AutoIT (from javascript)- Morse Code Generator - Generate morse code by tapping your spacebar!
Link to comment
Share on other sites

williamk, did you try my code? does it not work?

#include <file.au3>
#include <array.au3>
#include <Date.au3>
$dirpath = "D:\Datatrack\";set the path for the folder where the data extraction files are
$sfilter = "*.PRO"; set the filter for the files you want to extract data from
$FileList = _FileListToArray($dirpath, $sfilter, 1);get the list of files you want to extract data from
If @Error=1 Then
    MsgBox (0,"","No Files\Folders Found.")
    Exit
EndIf
$MA = $FileList[0]
While $MA >= 1
    $fd = StringLeft($FileList[$MA],8)
    MsgBox(4096, "emp detail records", "Number of emp detail records for profile is:" & $fd)
    $fdi = int($fd)
    ;MsgBox(4096, "emp detail records", "Number of emp detail records for profile is:" & $fdi)
    if $fdi < 20070802 or $fdi > 20070803 Then
        _ArrayDelete ($FileList, $MA)
    EndIf
    $MA = $MA - 1
WEnd
_ArrayDisplay ($FileList)

I think the problem with your code earlier is that you are indexing on $y instead of $x, and so you are actually skipping some values.

Hey BlueBear. Yep your code does work. Thanks much. You were right about my getting the $y and $x confused. Only problem is that the 0 element for the array still shows as 121, which could mess me up later on in my code when I reference the array. I think I will have to use the Ubound step method as suggested below to fix that.
Link to comment
Share on other sites

Not true.

Going through an array whilst deleting elements using a for loop is easy - I use it all the time -

...but you MUST go from back to front using -1 steps....

for $i = (UBound($array) - 1) to 1 step -1

Also, in my opinion, you're asking for trouble assuming dates are integers. Might not be too much of a problem in this case, but AutoIT has some amazing DATE functions that account for inconsistencies with our 4th dimension!! :)

Just my opinions, no critisism intended :)

Hey Andy,

Thanks much for your input. I agree, converting date strings to integers for purposes of filtering is not the ideal. I just didn't know of any other way to do it. How do I reset the array so the 0 element shows the correct number of elements in the array?

Edited by williamk
Link to comment
Share on other sites

Hey BlueBear. Yep your code does work. Thanks much. You were right about my getting the $y and $x confused. Only problem is that the 0 element for the array still shows as 121, which could mess me up later on in my code when I reference the array. I think I will have to use the Ubound step method as suggested below to fix that.

Ubound is the better way to go anyway. At least if current chatter is correct and using the [0] element to hold the element count may be phased out.

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

Ubound is the better way to go anyway. At least if current chatter is correct and using the [0] element to hold the element count may be phased out.

I remember reading a dev discussion about this.. and I think their concensus was while they wanted to remove the [0] element holding size thing... they decided it would break too many scripts currently out there so they were going to leave it. That is last I heard.
AutoIt Scripts:Aimbot: Proof of Concept - PixelSearching Aimbot with several search/autoshoot/lock-on techniques.Sliding Toolbar - Add a nice Sliding Toolbar to your next script. Click the link to see an animation of it in action!FontInfo UDF - Get list of system fonts, or search to see if a particular font is installed.Get Extended Property UDF - Retrieve a files extended properties (e.g., video/image dimensions, file version, bitrate of song/video, etc)
Link to comment
Share on other sites

I remember reading a dev discussion about this.. and I think their concensus was while they wanted to remove the [0] element holding size thing... they decided it would break too many scripts currently out there so they were going to leave it. That is last I heard.

Since when did we start worrying about breaking scripts around here? :) I've had lots of scripts broken by "advancements" in AutoIt. I use the term "advancements" a bit loosely as well because I didn't see all of them that way. However disagreement makes the world go around and sometimes we just have to adjust. To quote a quote as posted in Chat "Resistance is futile." Edited by GEOSoft

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

Hey Andy,

Thanks much for your input. I agree, converting date strings to integers for purposes of filtering is not the ideal. I just didn't know of any other way to do it. How do I reset the array so the 0 element shows the correct number of elements in the array?

Not tested, but I'd guess at:

$array[0] = Ubound($array)
- Table UDF - create simple data tables - Line Graph UDF GDI+ - quickly create simple line graphs with x and y axes (uses GDI+ with double buffer) - Line Graph UDF - quickly create simple line graphs with x and y axes (uses AI native graphic control) - Barcode Generator Code 128 B C - Create the 1/0 code for barcodes. - WebCam as BarCode Reader - use your webcam to read barcodes - Stereograms!!! - make your own stereograms in AutoIT - Ziggurat Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Box-Muller Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Elastic Radio Buttons - faux-gravity effects in AutoIT (from javascript)- Morse Code Generator - Generate morse code by tapping your spacebar!
Link to comment
Share on other sites

Not tested, but I'd guess at:

$array[0] = Ubound($array)oÝ÷ Ûú®¢×:'²+-z{ZºÚ"µÍÌÍØ^VÌHHXÝ[
    ÌÍØ^JKL
(traditionally, the counter element [0] has not been counted!)

Best, Randall

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