Jump to content

Array problems. plz help


iiyama
 Share

Recommended Posts

Hi,

I'm hoping that someone could give me a few tips on a problem I have regarding Arrays.

What I'm wanting to do is load a txt file into an Array then search for some text and find the line number of said text. I then need to add an extra 1 to the line number it finds.

So if it finds 5 then = 6, 11 = 12 and so on.

I guess I would then be able to use the variable containg the "line number" to then copy a line from one text file into another.

Example would be

[Test1]

1=jshdjsjsj

2=jshsjdjdhsjdh

[TESTa] <<<< find line number then add 1 to then put the 1=jsjshdfawe "line number" into a variable.

1=jsjshdfawe

2=363632

*** I would rather be able to copy 1=jsjshdfawe into a variable and not worry about adding a 1 to a number.

Otherwise if it were possible then just be able to copy 1=jsjshdfawe into a variable instead for [Testa].

I can normaly work out how to do things but Arrays do confuse me a bit.

I have attached what I have played with so far but can't think how to proceed. I have searched the forums but not been able to work out how to be able to do it. I hope someoneone would be be able to help me.

I have only been able to capture the line of the search string.

#include <Array.au3>

#include <file.au3>

Dim $aRecords

_FileReadToArray("c:\in\test.txt",$aRecords)

$sSearch = "[TESTa]" ;<== search text

$iIndex = _ArraySearch($aRecords, $sSearch, 0, 0, 0, 1)

If @error Then

If $iIndex = -1 Then

Exit

EndIf

Else

MsgBox(0, "Found", '"' & $sSearch & '" was found in the array at position ' & $iIndex & ".")

_FileWriteToLine("c:\in\NEWfile.txt", 1, '11 = ' & $iIndex, 1)

EndIf

Link to comment
Share on other sites

  • Moderators

iiyama,

Welcome to the AutoIt forum. :mellow:

You need to add 1 to the index you find to get the next line - like this:

#include <Array.au3>
#include <file.au3>

; Simulate loading file
Global $aRecords[4] = [3, "[TESTa]", "1=jsjshdfawe", "2=363632"]

;_FileReadToArray("c:\in\test.txt", $aRecords)

$sSearch = "[TESTa]" ;<== search text

$iIndex = _ArraySearch($aRecords, $sSearch);, 0, 0, 0, 1)

; Not found
If $iIndex = -1 Then
        Exit
Else
        
    MsgBox(0, "Found", '"' & $sSearch & '" was found in the array at position ' & $iIndex & ".")

    MsgBox(0, "WriteLine", $aRecords[$iIndex + 1])
    ;_FileWriteToLine("c:\in\NEWfile.txt", 1, '11 = ' & $iIndex, 1)
    
EndIf

Ask if anything is unclear. :(

There ia a good tutorial on arrays in the Wiki - you can find it here.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

Hi M23,

Thanks for the quick reply and welcome. I just had a look at your reply and it worked a treat.

I just needed to change a few things to my previous code that I got from yours and it was all good. I should now be able to use it for other things in the future.

It's nice to be able to kinda get my head around Arrays as normaly I try and code any other way to avoid them. :mellow:

Thanks again.

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