Jump to content
Sign in to follow this  
goldenix

[Solved] help with displaying arrays

Recommended Posts

goldenix

Hi

This should read all files into separate arrays so that _ArraySearch can be performed on each array lather.

1)

Does case sensitive search mean that the string which is being searched for, is converted into lowercase & the line of the array is also converted into lowercase & then compared?

2)

The arrays are nicely created, the problem is that for some reason they can not be accessed afterward`s & the script exits for some reason before displaying the ConsoleWrite.

#Include <File.au3>
#include <Array.au3>

Global $array
Global $array_list[1] ; 1 row in the beginning, contains list of all created arrays

$FileList=_FileListToArray(@ScriptDir)

;~ ==============================================================================
;~ List filenames into an array
;~ read each file contents into separate array
;~ Show contents of each file
;~ show list of all arrays created
;~ ==============================================================================
for $i = 1 to $FileList[0]

    $array = $array & $i

    Dim $array
    _ArrayAdd($array_list, '$array' & $i)   ; $array1,$array2 etc..

    _FileReadToArray($FileList[$i], $array) ; Read file to array

    _ArrayDisplay($array,"$array")          ; show contents of each file
next
_ArrayDisplay($array_list,"$array_list")    ; Show list of arrays created


;~ ==============================================================================
;~ Search each array
;~ ==============================================================================
for $i = 1 to $array_list[0]

    ConsoleWrite('Why is this message not displayed? ' & $i & @CRLF)

    $array_to_search = $array_list[$i]

    _ArrayDisplay($array_to_search,"$array_to_search")

    ;## convert search string, & array line into case sensitive & compare. Start from line 0 & finish at last line
    $iIndex = _ArraySearch($array_to_search, 'xxx', 1, $array_to_search[0], 1)

    If Not @error Then
        ConsoleWrite('xxx was found at line: ' & $i & ' ' & $array_to_search[$iIndex] & @CRLF) ; show line Nr & Contents of the line
    EndIf

Next
Edited by goldenix

My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]

Share this post


Link to post
Share on other sites
enaiman

Case sensitive = no conversion is being performed - it means that "c" and "C" are different characters

I don't know about your second issue but what I can tell you is: DO NOT mix numbers and strings in the same array - it will cause you only troubles. Also, array-of-arrays is another big trouble-maker.

Actually - that message is not displayed because $array_list[0] is "null"

Edited by enaiman

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites
goldenix

I did not pay attention to that cuz I thought like all autoit functions it will auto add the size of the array into line[0].

So i made the little change, but i still cant access those created arrays. any idea how can I do that?

I wanted to use autoit functions for this cuz the code will be really small if I do this. in my other code I tried it had several hundreds of lines of code, but hire I can probably do it in a less than 50.

I could put all into 1 array but then I need to use 2D arrays & autoit functions dont support this, also _filereadtoarray deletes all its previous data so I cant use this nether.

If there is a more efficient/shorter way to do this, please feel free to share your opinion.

#Include <File.au3>
#include <Array.au3>

Global $array
Global $array_list[1] ; 1 row in the beginning, contains list of all created arrays

$FileList=_FileListToArray(@ScriptDir)

for $i = 1 to $FileList[0]

    $array = $array & $i

    Dim $array
    _ArrayAdd($array_list, $i)   ; $array1,$array2 etc..

    _FileReadToArray($FileList[$i], $array) ; Read file to array
next
_ArrayDisplay($array_list,"$array_list")    ; Show list of arrays created


$array_list[0] = UBound($array_list)-1
;~ ==============================================================================
;~ Search each array
;~ ==============================================================================
for $i = 1 to $array_list[0]

    $array = $i

    _ArrayDisplay($array,"$array_to_search")
    ConsoleWrite('array search was displayed ' & $array & @CRLF)
Next
Edited by goldenix

My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]

Share this post


Link to post
Share on other sites
Melba23

goldenix,

I would do it this way: :idea:

#include <File.au3>
#include <Array.au3>

Global $array_list[1] ; 1 row in the beginning, contains list of all created arrays

$FileList = _FileListToArray(@ScriptDir, "*", 1)

; Create an array to hold the other arrays
Global $aArrays[$FileList[0] + 1] = [$FileList[0]]

For $i = 1 To $FileList[0]

    _ArrayAdd($array_list, $FileList[$i])

    _FileReadToArray(@ScriptDir & "\" & $FileList[$i], $aArrays[$i]) ; Read file to array

Next

;~ ==============================================================================
;~ Search each array
;~ ==============================================================================

For $i = 1 To $aArrays[0]

    _ArrayDisplay($aArrays[$i], "Search " & $array_list[$i])
    ConsoleWrite('array search was displayed ' & $array_list[$i] & @CRLF)

Next

Notice that I am storing the arrays WITHIN an array - it makes it much easier to manage the names.

Now you are going to say: "But you cannot put arrays in arrays! :( ". Well, you can, but you need to be aware of what you are doing - from the Help file:

"Especially the use of an Array inside another Array will severely affect the execution speed of your script."

So you may find that your script is not as fast as it could be. But I prefer this method to the use of Assign/Eval/Execute to create the array name.

Try it and see if it is fast enough for your needs. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
GEOSoft

I did not pay attention to that cuz I thought like all autoit functions it will auto add the size of the array into line[0].

That is often an incorrect assumption to make. Many of the AutoIt functions return a zero based array. Hence the recommendation of using Ubound($aArray) -1, instead of $aArray[0] for the array dimension.

For examples, take a look at any of the WinGet*() or the ControlGet*() functions.

If you don't like writing the lines like

For $i = 0 To Ubound($aArray) -1

Then you can do

$iUbound = Ubound($aArray) -1
For $i = 0 To $iUbound

Edit: Another time it is wrong is when returning an array from StringRegExp(), they will always be 0 based. therefore For $i = 1 To $aArray[0] will always be wrong. It will miss the first element and the dimension is incorrect.

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!"

Share this post


Link to post
Share on other sites
goldenix

Is it possible for _filereadtoarray to read my file into the array in lowercase so I can use lowercase arraysearch function?

This part is confusing me, can you please explain?

How should I visualize this?

Global $aArrays[$FileList[0] + 1] = [$FileList[0]]

Look at ;~ Search each array part:

I tried to mod my code, but in the end im getting this error. Why cant I access the array to display the line?

$searcharray[$iIndex] & @CRLF)
$searcharray[^ ERROR

#include <File.au3>
#include <Array.au3>

Global $Filename[1] ; 1 row in the beginning, contains list of all created arrays

$FileList = _FileListToArray(@ScriptDir, "*", 1)

; Create an array to hold the other arrays
Global $aArrays[$FileList[0] + 1] = [$FileList[0]]

For $i = 1 To $FileList[0]

    _ArrayAdd($Filename, $FileList[$i])

    _FileReadToArray(@ScriptDir & "\" & $FileList[$i], $aArrays[$i]) ; Read file to array

Next
;~ ==============================================================================
;~ Search each array
;~ ==============================================================================
For $i = 1 To $aArrays[0]

    $searcharray = $aArrays[$i]

    $iIndex = _ArraySearch($searcharray, 'xxxxxxxxxxxxxxx',1)
    ConsoleWrite($iIndex & '    ' & $Filename[$i] & @CRLF)

    If Not @error Then
        ConsoleWrite($iIndex & '    ' & $Filename[$i] & $searcharray[$iIndex] & @CRLF); index,file, line
    EndIf
Next
Edited by goldenix

My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]

Share this post


Link to post
Share on other sites
Melba23

goldenix,

Q 1: Is it possible for _filereadtoarray to read my file into the array in lowercase so I can use lowercase arraysearch function?

Ans 1: Do you see an option in the Help file to allow that? No. But what is this "lowercase arraysearch function"? Just leave the "case-sensitive" flag at 0 and _ArraySearch will find the match whether it is lower or uppercase - or set it to 1 and use case sensitive strings in the search.

Q 2: How should I visualize this: Global $aArrays[$FileList[0] + 1] = [$FileList[0]]?

Ans 2: The number of files returned from _FileListToArray is in $FileList[0]. We need a further array to hold the arrays which we will create with _FileReadToArray, which needs as many elements as we have files. Here we declare an array which has 1 more element than the number of files so the element numbers if the 2 arrays match - so the file whose name is in $FileList[n] has its contents in the array $aArrays[n]. And because you seem to like having a count in the first element, I put it there! :idea:

Q 3: Why cant I access the array to display the line?

Ans 3: Because @error is set after each command and you have a ConsoleWrite command before you test. You need to test @error immediately after the _ArraySearch command like this:

$iIndex = _ArraySearch($searcharray, 'Xxxxxxxxxxxxxxx', 1)
If Not @error Then
    ConsoleWrite($iIndex & '    ' & $Filename[$i] & $searcharray[$iIndex] & @CRLF); index,file, line
Else
    ConsoleWrite($iIndex & '    ' & $Filename[$i] & "Not found" & @CRLF)
EndIf

All clear? :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
goldenix

Let me see if this is the same as your code:

I read your explanation several times but I still dont get why we need to add +1 ?

$FileList[0] = 26
    
    $X = $FileList[0]
    
    $X = $X +1
    
    $FileList[0] = $X   ; $FileList[0] =27 now

    Global $aArrays[$X] ; declare array with 27 rows

Global $aArrays[$FileList[0] + 1] = [$FileList[0]]
Edited by goldenix

My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]

Share this post


Link to post
Share on other sites
Melba23

goldenix,

We need at least as many elements in $aArray as there are files to read into arrays. But it makes it much easier to index if we can use the same value for the filename and the associated array. So we need to have an additional element in $aArrays to compensate for the count of files that is in $FileList[0]:

Array:    $FileList            $aArrays

[0]       Count of files = n   Unused
[1]       File 1               Array from File 1
[2]       File 2               Array from File 2
...
...
[n]       File n               Array from File n

Elements  n + 1                n + 1

So we have to declare $aArray with n + 1 elements, which gives us $aArray[$FileList[0] + 1].

Does this make it any clearer? :idea:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
goldenix

EDT: okkey this still does not work, for some reason array search takes only 0,6 seconds instead of 30 sec & cant find the lines im searching for. anyway ill look further into this.

Edit2: im confused, its seems it works after all & it searches everything within 1,3 seconds. my old script with fileopen & filereadlines took around 20 sec. will keep continue testing....

Okey I understood now, hire is the thing,

$FileList = _FileListToArray(@ScriptDir, "*", 1)
_ArrayDisplay($FileList,'')

Imagine that $FileList returns 3 files. This array size will then be 3 rows. Like it should be.!

$FileList[0] = 3
$FileList[1] =  File_1.txt
$FileList[2] =  File_2.txt
$FileList[3] =  File_3.txt

If we declare this array like this: it will look like:

Global $aArrays[$FileList[0]]
_ArrayDisplay($aArrays,'')

$FileList[0] = 
$FileList[1] = 
$FileList[2] =

Note that I have this code afterwards: ...Now imagine what will happen if we start adding stuff to $aArrays. We start counting from 1 not 0 . Result = Array Dimension range exeeded. This why we need +1

For $i = 1 To $FileList[0]
_FileReadToArray(....$FileList[$i], $aArrays[$i])

so ye thanx for the explanation :idea:

Edited by goldenix

My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]

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  

×