Sign in to follow this  
Followers 0
atnextc

Help With StringBetween With FileReadToArray

13 posts in this topic

Hey guys,

I have a problem I have run into on a script that was going rather well given the limited amount of scripting / programming knowledge that I have.  The issue that I have run into seems to be a catch 22 in that I have 2 files which contain router interface commands for our equipment.

I basically need to declare a variable in one for loop, and pass that same variable to either another function or another for loop.

What I am getting now is "Subscript used with non-Array variable.:" on $asideinterface for the _bside function

I"m basically trying to grab this data from both files.  Test.txt would be the A side, and Test2.txt would be the Z side.  

$asideinterface = _StringBetween ($ainput[$a],"interfaces ", " description")

$circuitid =  _StringBetween ($ainput[$a],"""Cox;",";C4")

$zsiderouter = _StringBetween ($ainput[$a],"C4 ;",";")

$zsideinterface = _StringBetween ($ainput[$a],";", ";")   <-----I'm trying to grab whats between ; and ; towards the end of the file, but i can't tell it not to use the first instance of what it finds using _StringBetween

 

Sample code posted below.

Sample code that can be used for "test.txt" and "test2.txt" listed in code.

Test.txt:

set interfaces xe-0/0/6 description "Cox;13001.GE10.CHCGILDTJBW.PRVERIXYJAW;C4 CHGOBPRJ02;xe-0/0/0;MR;10GE TO CHGOBPRJ02 #1;"

Test2.txt:

set interfaces xe-0/0/0 description "Cox;13001.GE10.CHCGILDTJBW.PRVERIXYJAW;C4 PROVBBRJ01;xe-0/0/6;MR;10GE To PROVBBRJ01;"
#include <file.au3>
#include <String.au3>

Local $a,$afile, $bfile, $ainput, $binput,$circuitid, $asiderouter, $zsiderouter, $asideinterfaces,$zsideinterfaces

$afile = @ScriptDir & "\test.txt"
$bfile = @ScriptDir & "\test2.txt"

_FileReadToArray($afile,$ainput)
_FileReadToArray($bfile,$binput)

For $b = 0 To UBound($binput)-1
    $zsideinterfaces = _StringBetween ($binput[$b],"interfaces ", " description")
    $asideinterfaces = _StringBetween ($binput[$b],";", ";")  <-----I'm trying to grab whats between ; and ; towards the end of the file, but i can't tell it not to use the first instance of what it finds using _StringBetween
    if stringinstr($binput[$b],"description") Then
    filewrite (@ScriptDir & "\bsideinterfaces.txt",$zsideinterfaces[0]& @CRLF)
    filewrite (@ScriptDir & "\bsideinterfaces.txt",$asideinterfaces[0]& @CRLF)
    EndIf
Next

For $a = 0 To UBound($ainput)-1
    $asideinterfaces = _StringBetween ($ainput[$a],"interfaces ", " description")
    $zsideinterfaces = _StringBetween ($ainput[$a],";", ";;")
    if stringinstr($ainput[$a],"description") Then
    filewrite (@ScriptDir & "\asideinterfaces.txt",$asideinterfaces[0]& @CRLF)
    filewrite (@ScriptDir & "\asideinterfaces.txt",$zsideinterfaces[0]& @CRLF)
    EndIf
Next




    _bside()
func _bside()
For $b = 0 to UBound($binput)-1

$zsideinterfaces = _StringBetween ($binput[$b],"interfaces ", " description")
$circuitid = _StringBetween ($binput[$b],"""Cox;",";C4")
;$circuitid = _StringBetween($binput[$b],";", @CRLF)
local   $asiderouter = _StringBetween ($binput[$b],";C4 ",";")
local   $asideinterfaces = _StringBetween ($binput[$b],";", ";")<-----I'm trying to grab whats between ; and ; towards the end of the file, but i can't tell it not to use the first instance of what it finds using _StringBetween

if stringinstr($binput[$b],"description") Then

_FileWriteToLine(@ScriptDir & "\test2.txt", $b, "set interfaces " &$zsideinterfaces[0]& " description ""Cox;"&$circuitid[0]&";C4 "&$asiderouter[0]&";"& $asideinterfaces[0] &";MR;10GE To " & $asiderouter[0] &";""", 1)
EndIf

Next
EndFunc

_aside()

func _aside()
For $a = 1 to UBound($ainput) -1

   $asideinterfaces = _StringBetween ($ainput[$a],"interfaces ", " description")
Local   $circuitid = _StringBetween ($ainput[$a],"""Cox;",";C4")
local   $zsiderouter = _StringBetween ($ainput[$a],";C4 ",";")
local   $zsideinterfaces = _StringBetween ($ainput[$a],";", ";MR;10GE")
if stringinstr($ainput[$a],"description") Then
         _FileWriteToLine(@ScriptDir & "\test.txt", $a, "set interfaces " &$asideinterfaces[0]& " description ""Cox;"&$circuitid[0]&";C4 "&$zsiderouter[0]&";"& $zsideinterfaces[0] &";MR;10GE To " & $zsiderouter[0] &";""", 1)
         EndIf
     Next
     EndFunc

Please let me know if I need to be more clear I know this is kind of all over the place.

Thanks for any help in advance.

Share this post


Link to post
Share on other sites

yea typo when i put it in there. its not that way in the actual script.

Share this post


Link to post
Share on other sites

the error is typical when you are calling a function that is supposed to return an array - for example, _StringBetween() - but some error has occurred, and the function did not return the array.

check @error after calling _StringBetween()

Share this post


Link to post
Share on other sites

Appears to work for me.

Anything added or changed is indicated.

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

Local $a, $afile, $bfile, $ainput, $binput, $circuitid, $asiderouter, $zsiderouter, $asideinterfaces, $zsideinterfaces

$afile = @ScriptDir & "\test.txt"
$bfile = @ScriptDir & "\test2.txt"

_FileReadToArray($afile, $ainput)
_ArrayDisplay($ainput);<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
_FileReadToArray($bfile, $binput)
_ArrayDisplay($ainput);<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

For $b = 1 To $binput[0];<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    $zsideinterfaces = _StringBetween($binput[$b], "interfaces ", " description")
    _ArrayDisplay($zsideinterfaces)
    $asideinterfaces = _StringBetween($binput[$b], ";", ";") ;< - - - - -I'm trying to grab whats between ; and ; towards the end of the file, but i can' t tell it Not To use the first instance of what it finds using _StringBetween
    _ArrayDisplay($asideinterfaces)
    If StringInStr($binput[$b], "description") Then
        FileWrite(@ScriptDir & "\bsideinterfaces.txt", $zsideinterfaces[0] & @CRLF)
        FileWrite(@ScriptDir & "\bsideinterfaces.txt", $asideinterfaces[0] & @CRLF)
    EndIf
Next

For $a = 1 To $ainput[0];<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    $asideinterfaces = _StringBetween($ainput[$a], "interfaces ", " description")
    $zsideinterfaces = _StringBetween($ainput[$a], ";", ";")
    If StringInStr($ainput[$a], "description") Then
        FileWrite(@ScriptDir & "\asideinterfaces.txt", $asideinterfaces[0] & @CRLF)
        FileWrite(@ScriptDir & "\asideinterfaces.txt", $zsideinterfaces[0] & @CRLF)
    EndIf
Next




_bside()
Func _bside()
    For $b = 0 To UBound($binput) - 1

        $zsideinterfaces = _StringBetween($binput[$b], "interfaces ", " description")
        $circuitid = _StringBetween($binput[$b], """Cox;", ";C4")
        ;$circuitid = _StringBetween($binput[$b],";", @CRLF)
        Local $asiderouter = _StringBetween($binput[$b], ";C4 ", ";")
        Local $asideinterfaces = _StringBetween($binput[$b], ";", ";") ;< - - - - -I'm trying to grab whats between ; and ; towards the end of the file, but i can' t tell it Not To use the first instance of what it finds using _StringBetween

        If StringInStr($binput[$b], "description") Then

            _FileWriteToLine(@ScriptDir & "\test2.txt", $b, "set interfaces " & $zsideinterfaces[0] & " description ""Cox;" & $circuitid[0] & ";C4 " & $asiderouter[0] & ";" & $asideinterfaces[0] & ";MR;10GE To " & $asiderouter[0] & ";""", 1)
        EndIf

    Next
EndFunc   ;==>_bside

_aside()

Func _aside()
    For $a = 1 To UBound($ainput) - 1

        $asideinterfaces = _StringBetween($ainput[$a], "interfaces ", " description")
        Local $circuitid = _StringBetween($ainput[$a], """Cox;", ";C4")
        Local $zsiderouter = _StringBetween($ainput[$a], ";C4 ", ";")
        Local $zsideinterfaces = _StringBetween($ainput[$a], ";", ";MR;10GE")
        If StringInStr($ainput[$a], "description") Then
            _FileWriteToLine(@ScriptDir & "\test.txt", $a, "set interfaces " & $asideinterfaces[0] & " description ""Cox;" & $circuitid[0] & ";C4 " & $zsiderouter[0] & ";" & $zsideinterfaces[0] & ";MR;10GE To " & $zsiderouter[0] & ";""", 1)
        EndIf
    Next
EndFunc   ;==>_aside

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

thanks JohnOne For the reply. While it is not erroring, its still not grabbing the correct information for the Zside interface. I believe i've done what you posted above before while trying to get this to work, thinking heck yea finally figured it out.
the only thing i'm interested in getting that i can't seem to get properly is in bold below.

set interfaces xe-0/0/6 description "Cox;13001.GE10.CHCGILDTJBW.PRVERIXYJAW;C4 CHGOBPRJ02;xe-0/0/0;MR;10GE TO CHGOBPRJ02;"

What the script above does is grabs everything between ; and ; and writes that back into the file resulting in the following:

set interfaces xe-0/0/6 description "Cox;13001.GE10.CHCGILDTJBW.PRVERIXYJAW;C4 CHGOBPRJ02;13001.GE10.CHCGILDTJBW.PRVERIXYJAW;C4 CHGOBPRJ02;xe-0/0/0;MR;10GE TO CHGOBPRJ02;"

what this is essentially doing is copying the "circuitID" field and the "zsiderouter" field 2 times and pasting it back in the file. I've also tried doing an if stringinstr = the above syntax to basically remove the dupes but it didn't resolve the issue.

Any ideas are more than welcome :) i've been working like 20+ hours just trying to get this one stupid part right, and I can't move on with the script unfortunately until this is correct.

Edited by atnextc
Fixed font size

Share this post


Link to post
Share on other sites

atnextc,

Please use standard font size when you post - making it as small as it was will not help you, as many people will not even bother to read it. ;)

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

Melba23 thanks, didn't even notice that.  Not sure how that happened :)

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Can't this be done with just a small regex ?

; before
$str = 'set interfaces xe-0/0/6 description "Cox;13001.GE10.CHCGILDTJBW.PRVERIXYJAW;C4 CHGOBPRJ02;xe-0/0/0;MR;10GE TO CHGOBPRJ02;"'

$res = StringRegExpReplace($str, '(.+?);(.+?;.+?);(.+)', "$1;$2;$2;$3")
Msgbox(0,"", $res)

; after
; set interfaces xe-0/0/6 description "Cox;13001.GE10.CHCGILDTJBW.PRVERIXYJAW;C4 CHGOBPRJ02;13001.GE10.CHCGILDTJBW.PRVERIXYJAW;C4 CHGOBPRJ02;xe-0/0/0;MR;10GE TO CHGOBPRJ02;"
Edited by mikell

Share this post


Link to post
Share on other sites

mikell, thanks, what i'm looking for is to parse out only the second interface that is listed in the description which in the sample example provided is xe-0/0/0.  I"m not familiar with regex.  I imagine there is a way to grab that out using them though.

Share this post


Link to post
Share on other sites

well probably not the best method. I was able to do what I needed using:

$str = 'set interfaces xe-0/0/6 description "Cox;13001.GE10.CHCGILDTJBW.PRVERIXYJAW;C4 CHGOBPRJ02;xe-0/0/0;MR;10GE TO CHGOBPRJ02;"'
$res = StringSplit($str,";",2)
Msgbox(0,"", $res[3])

Share this post


Link to post
Share on other sites

Oh yes, on reading your previous post I thought that the purpose was to get the whole 2nd string  by modifying the first one

But to grab 'xe-0/0/0' only StringSplit is obviously the best way :)

Share this post


Link to post
Share on other sites

So ran into another issue semi related. 

Basically i'm reading a file into an array in 1 function, and its running a certain number of times, lets say 6. The array has lets say 10 items in it.

Then I'm reading that same file into another array in another function later on in the script.  How can I "pick up" at value 7 and have it loop the remaining 3 times in this example till it reaches the end of the array.

I can provide examples of what i've tried if need be.

Thanks

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