Sign in to follow this  
Followers 0
NoobieAutoitUser

Need some help with Arrays, and searching them.

9 posts in this topic

I have tried (it works mostly) to create a script that opens a text file, and looks for specific data 'www.test.com'. Then it strips out everything that does not contain 'www.test.com'. Then outputs to a text file that only has the lines with 'www.test.com' in them. I'm trying to duplicate some of the functionality of SED (native to Linux, but I use the windows version).
I created a script and it works beyond my dreams, I was ecstatic :)
Then it dawned on me that I need to put in some error correction. In case the text file did not contain the data I was looking for. That is what slowed me down by a lot.
The script works great except I can't figure out to search for a string in the array to see if it exists. That being the error correction.
Most of my script I understand what I'm doing, but these 'array' things, are killing me :(
I'm also getting stuck on the a loop. I want it to search for a string, if not found then 'error box', if found then to write a file.
If any kind soul could point me in the right direction, I would appreciate it a lot.

#include <file.au3>
#include <Array.au3>
Dim $array
$s = _FileReadToArray("testreppb.txt", $array);read the text file to an array of lines
FileDelete ( "converted.txt" )


$value = InputBox("Input Box","Text to search")
ClipPut($value)
$bak = ClipGet()
; MsgBox(0, "Clipboard contains:", $bak)
$file = FileOpen("converted.txt", 2);open the file to write the results to
$searchstring = $bak
MsgBox(0, "", $searchstring)

_ArrayDisplay($array, "$avArray")
Local $avArray = $array
$searchstring = InputBox("_ArraySearch() demo", "String to find?")
$iIndex = _ArraySearch($avArray, $searchstring)
; MsgBox(0, "test",  $iIndex)
If @error Then
MsgBox(0, "Not Found",  $searchstring)
Else
MsgBox(0, "Found",  $searchstring)
EndIf

for $n = 0 to $array[0] 
   if StringInStr($array[$n],$searchstring) then
          MsgBox(0, "file write", $array[$n]) 
   endif 
next

Share this post


Link to post
Share on other sites



Why not do something easier.

Local $sDatos=fileread("testreppb.txt")
Local $stringtosearch=""


If StringInStr($sDatos,$stringtosearch) Then
    filewrite("output.txt",$stringtosearch & @CRLF)
EndIf

saludos

Share this post


Link to post
Share on other sites

arrays are, at first, a bit confusing........

however, they are not difficult and they are very, very powerful, so getting a grip on them is pretty crucial to programming.

Danyfirex does show you a much simpler way to go about what you are looking to do, however, you asked about searching inside an array, so I wanted to address that..........

http://www.autoitscript.com/autoit3/docs/libfunctions/_ArrayBinarySearch.htm is your friend.

AutoIt isn't straightforward on searching in the array, but this is how it is done, and it works great.  Just be sure to use _ArraySort first (as shown in the documentation).

If arrays confuse you, think of them in this way (it helped me a lot when I was first starting....);

Visualize (or grab) a piece of standard, lined paper (like you used in school to practice writing) - something like.........

 

 

---------------------------------------------------------

---------------------------------------------------------

---------------------------------------------------------

etc.

 

That is your 'blank' array.

you name the 'paper' (array) in the code so you can remember which page it is on  (in your case, you named it $array, that is fine)

 

$array

 

---------------------------------------------------------

---------------------------------------------------------

---------------------------------------------------------

etc.

now, lets put something IN this array - and when you do, make sure you give it a 'line' number for each bit of data...

 

$array

1 firstdata

---------------------------------------------------------

2 seconddata

---------------------------------------------------------

3 thirddata

---------------------------------------------------------

etc.

 

OK, now we have an array with stuff in it.

How to get it out?

Just call it up, by 'page' and 'line' name!  In AutoIt, you would just use $array[X] where the 'X' is the number you want.

let's say you want to write it to the console (using SciTE editor, you will see this on the bottom of the screen)

ConsoleWrite($array[3] & @CRLF) 

would result in "thirddata" being shown in the console window.

Want to search for 'thirddata' to find out where it is in the array? (of course, in our example, we know where it is, but for 'real world' scenarios, we don't always know, so we need to search.)  

That is when you use the _ArrayBinarySearch function (see the docs)

Hope this helps clear up how SIMPLE arrays really are (they are just like using school paper!)  Of course, they can get more complex, but that will come along for you when you really need to use it (I've used arrays for years like the above and rarely get into the more complex ones, so don't rush yourself!)

Share this post


Link to post
Share on other sites

well @TechCoder is right my answer was not as you want.  so you're searching tow time. You can do it so:

#include <file.au3>
#include <Array.au3>
Dim $array
local $s = _FileReadToArray("1.txt", $array);read the text file to an array of lines
FileDelete ( "converted.txt" )


local $value = InputBox("Input Box","Text to search")
ClipPut($value)


local $file = FileOpen("converted.txt", 2);open the file to write the results to
local $searchstring = $value
MsgBox(0, "", $searchstring)

;_ArrayDisplay($array, "$avArray")


for $n = 0 to $array[0]
   if StringInStr($array[$n],$searchstring) then
          MsgBox(0,"Array index:" & $n,"file write", $array[$n])
   endif
next

saludos

Share this post


Link to post
Share on other sites

@Danyfirex
Thanx a lot for your replies. I will be able to look over the differences between my script and yours. Yours looks a lot cleaner then mine, though that has a lot to do with me trying to 'troubleshoot' mine :) Thanx again for your help :) BTW: what is tow time?

@TechCoder
A billion thanx as to laying the ground work for how array's work :) I read a while back somewhere that peeps new to programming struggle with arrays. That is why I was trying to script with this one, so I can slowly 'master it'. I have saved your reply as a text file so I can go back to it when I get lost again :)
So lets say I have some data 'folder1folder2folder3 that I want to 'read' into an array. First I create an array calling it 'pathtofolder'. I then separate the 'data' from the ''. So then 'line 1' would be 'folder1' in the array, and 'line 2' would be 'folder2', and so on and so forth?

Share this post


Link to post
Share on other sites

@NoobieAutoitUser I mean you are using array search (this does some loop) and then your are looping again when you (for $n = 0 to $array[0]) . so only use one.  

sorry I'm not english native speaker.  Always look into help file (great explained)

saludos 

Share this post


Link to post
Share on other sites

@Danyfirex

Thanx a lot for your replies. I will be able to look over the differences between my script and yours. Yours looks a lot cleaner then mine, though that has a lot to do with me trying to 'troubleshoot' mine :) Thanx again for your help :) BTW: what is tow time?

@TechCoder

A billion thanx as to laying the ground work for how array's work :) I read a while back somewhere that peeps new to programming struggle with arrays. That is why I was trying to script with this one, so I can slowly 'master it'. I have saved your reply as a text file so I can go back to it when I get lost again :)

So lets say I have some data 'folder1folder2folder3 that I want to 'read' into an array. First I create an array calling it 'pathtofolder'. I then separate the 'data' from the ''. So then 'line 1' would be 'folder1' in the array, and 'line 2' would be 'folder2', and so on and so forth?

first, let me clarify......   "tow time" = TWO TIMES (@Danyfirex explained it though I thought you might still miss that finer point.  I've been married to a Russian woman for 20 years and I'm now living in Ecuador, so I'm used to reading and speaking 'non-native', though not everyone is)

on the array - glad my crude diagram helped (again, living with and teaching technology to non-native English speakers has given me some 'talent' for KISS training......  ;)

To your question - it could be that you would want to create an array like that, though what you are doing in your example is creating an array of folder names to a path - not at all what I would call 'common' use, but again, you could - after all, it is just "words on a page" (as I like to call it - others call it "data"....) - and while those 'words' mean something to someone, part of your task as a programmer is to forget about what those things do/say/spell/mean and simply manipulate them around as you need them.  (this is probably a different concept for you, so don't worry if you don't grasp it first time, but the closer you get to separating yourself from the 'meaning' of what is moving around, the faster you can learn what arrays (and database segments, etc.) can do for you.  

They are just storage locations for 'words' (or pictures, or....... - data!)

keeping my piece-o-paper vision handy, look at arrays another way - think of a school locker, or perhaps a row of post office boxes, stacked on top of each other.  For our example, think of ONE row only!  (either vertical or horizontal, though typically arrays are thought of in a vertical stack - like the piece of paper....)  Arrays can grow to many rows and columns (if you have dealt with spreadsheets, these terms are easier to understand), but we are talking about a simple array, one 'stack' (or column if you prefer...)

Now, getting back to your example of 'folder1folder2folder3 - if what you are doing is breaking up pathnames, then you seem to be getting the idea very well.  You break the data by whatever delimiter you can (which sometimes is a or comma, or even X characters - whatever it is...) and you put the bits, one per each location, into each 'storage box' (or locker, or 'stack location' - again terms are not important other than your understanding).

So, in your example, if you have an array called $pathtofolder and you did a split of the pathname 'folder1folder2folder3 by , you would have

$pathtofolder[0] = 3   <<======  this is how AutoIt handles things most of the time (it is a bit confusing because it is not always done this way - see below)

$pathtofolder[1] = folder1

$pathtofolder[2] = folder2

$pathtofolder[3] = folder3

So, yes, you are on the right track, I think.

Now, I won't go into detail on the [0] array bit - AutoIt handles it in various ways and you should read the docs for some details on when/why/how it might change, but know that 1) you have control over how it works and 2) AutoIt gives variations that are very powerful when used correctly (i.e., I was very confused at first, but after a few days of messing with it, I have grown to really like the use of the [0] array bit and think it is quite a handy tool).

Keep studying on arrays and think of stacks of boxes (or whatever) and then you can put something in each one, as/where you like.  As long as you have some way to keep track of where that thing is, you can easily go get it, use it and put it back (changed or not - your decision) in the same place, or add it to another box, throw it away, etc. (i.e., manipulate the data as you like)

Hope this helps.

1 person likes this

Share this post


Link to post
Share on other sites

@TechCoder

Again thanx for your input :)
Over the last few years I have started to get real good at the 'command line' (Windows) and manipulating data that way. The 'command line is 'very limited' on what you can do.
I started messing with Autoit a couple years ago, scripting some little stuff, and expanding my understanding of it. IMO I have written some pretty killer scripts. and I love the power of Autoit. Though I don't really understand some of it, I 'just' get it to work :)
I have some experience with 'spread sheets' so I grasp the rows and columns (to a point).
I also have learned how 'delimiters' work using the 'for' command of Windows, so I kinda understand how to 'break' my data up into pieces to work with. Delimiters can be anything  '/,x, csv'.
$pathtofolder[0] = 3
The above creates an Array of 3 blocks in a column? Also you can not have data at the root '[0]' of the Array? What if you don't know how many 'blocks' you going to need in a column?
$pathtofolder[1] = folder1
$pathtofolder[2] = folder2
$pathtofolder[3] = folder3
The above the is assigned into a different box's after being chopped by the delimiter?

Right now the biggest thing holding back my script is 'how do you search an Array' using '_ArrayBinarySearch'.

_ArraySort($avArray)
; Lookup existing entry
$iKeyIndex = _ArrayBinarySearch($avArray, $searchstring,1)
If Not @error Then
    MsgBox(0, 'Entry found', ' Index:' & $iKeyIndex)
Else
    MsgBox(0, 'Entry Not found', ' Error:' & @error)
EndIf

If the data is 'not found' it works perfect like that (it errors and does not run the rest of script). Though when the data is 'found' it still gives me an error and proceeds to run the rest of my script. I have spent a week after work, trying to figure this one out. I feel like I'm missing something really small.
BTW I do read the help files when I get stuck. sometimes though you need to ask for help :)
Again thanx for your input and direction :)

Share this post


Link to post
Share on other sites

 

$pathtofolder[0] = 3

The above creates an Array of 3 blocks in a column? 

yes. that is how AutoIt does it.  Not all programming languages do it this way (nor does AutoIt always make arrays in this way....) and as I said, it is a source of confusion for me as well.

 

Also you can not have data at the root '[0]' of the Array?

 

not if this is the 'type' of array you are building in AutoIt (again, I'm not yet expert on why {and the reasoning behind it is really not important, only understanding that there are two types is} and only see it as the use - and power that is possible - for each).

building an array that keeps track of the number of elements in [0] is actually quite useful.  No need to ever call another function to see how many there are - just use the [0] element;

for $x = 1 to $array[0]

 

What if you don't know how many 'blocks' you going to need in a column?

 

AutoIt ALSO allows you to build arrays in this way.  I'm only learning the instances, though take a look in the help file at examples (and try them - one of the best ways to learn - I keep the help file open constantly and when I need a function, just go and grab an example - works quite well)

_ArrayAdd, _ArrayDelete, _ArrayPop, _ArrayPush, _ArrayInsert

 

when the data is 'found' it still gives me an error and proceeds to run the rest of my script. .......I feel like I'm missing something really small.

 

you didn't give all your code, so I am a bit lost and mostly because you say 

 

when the data is 'found' it still gives me an error and proceeds to run the rest of my script.

just what ERROR do you get?

from what code you gave (presuming you came into it with $avArray, $searchstring intact, a 'found' should give you a MsgBox (NOT an 'error'....) and then, yes, continue on with the code.

You really 'MUST' give details whenever you discuss the word "error" - always be sure to tell someone exactly what that error says (and looks like - could be that a certain type of display is used for a different message, etc.)  

When you write code for others to use, you will see how frustrating it is to have someone tell you "it doesn't work" or "I get an error message" - absolutely no way to help them - as much as you would like.........

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