Sign in to follow this  
Followers 0
charon

Help with _ArrayInsert in a loop

7 posts in this topic

I am trying to inert a new value into an existing array

I have an array that looks like this before I change it:

Row|Col 0
[0]|10
[1]|10
[2]|JourneyFrontiersJourney-After The Fall.mp3
[3]|JourneyFrontiersJourney-Ask the Lonely.mp3
[4]|JourneyGreatest_HitsJourney-Any Way You Want It.mp3
[5]|JourneyGreatest_HitsJourney-Dont Stop Believin.mp3
[6]|QueensrycheEmpireQueensryche-01-Best I Can.mp3
[7]|QueensrycheEmpireQueensryche-02-The Thin Line.mp3

I am trying to make it look like this:   The insert is only when the artist changes and then I insert: new channel2 broadcast enabled loop

Row|Col 0
[0]|10
[1]|10
[2]|setup channel1 input "C:atestJourneyFrontiersJourney-After The Fall.mp3"
[3]|setup channel1 input "C:atestJourneyFrontiersJourney-Ask the Lonely.mp3"
[4]|setup channel1 input "C:atestJourneyGreatest_HitsJourney-Any Way You Want It.mp3"
[5]|new channel2 broadcast enabled loop
[6]|setup channel2 input "C:atestQueensrycheEmpireQueensryche-02-The Thin Line.mp3"
[7]|setup channel2 input "C:atestQueensrycheEmpireQueensryche-03-Jet City Woman.mp3

Here is what I get:  A big mess

Row|Col 0
[0]|10
[1]|10
[2]|setup channel1 input "C:atestJourneyFrontiersJourney-After The Fall.mp3"
[3]|setup channel1 input "C:atestJourneyFrontiersJourney-Ask the Lonely.mp3"
[4]|setup channel1 input "C:atestJourneyGreatest_HitsJourney-Any Way You Want It.mp3"
[5]|new channel2 broadcast enabled loop
[6]|new channel3 broadcast enabled loop
[7]|new channel4 broadcast enabled loop
[8]|new channel5 broadcast enabled loop
[9]|new channel6 broadcast enabled loop
[10]|new channel7 broadcast enabled loop
[11]|setup channel1 input "C:atestJourneyGreatest_HitsJourney-Dont Stop Believin.mp3"
[12]|setup channel7 input "C:atestsetup channel6 input "C:atestsetup channel5 input "C:atestsetup channel4 input "C:atestsetup channel3 input "C:atestsetup channel2 input "C:atestQueensrycheEmpireQueensryche-01-Best I Can.mp3
[13]|QueensrycheEmpireQueensryche-02-The Thin Line.mp3
[14]|QueensrycheEmpireQueensryche-03-Jet City Woman.mp3

 

Here is my loop:

$iPosition = StringInStr($aArray[2], "\")
    $sString[0] = StringLeft($aArray[2], $iPosition -1) ; Retrieve 5 characters from the left of the string.
    $sString[1] = StringLeft($aArray[2], $iPosition -1)
    ;MsgBox(0, "", "string: " & $sString[0])

 For $i = 2 To $aArray[0] +1
    $iPosition = StringInStr($aArray[$i], "\")
    $sString[1] = StringLeft($aArray[$i], $iPosition -1) ; Retrieve 5 characters from the left of the string.
    
          If $sString[0] <> $sString[1] Then ;if artist doesn't match then we increment channel and insert new value at position $i - 1
        ;MsgBox(0, "", "change found" & @CR & "string[0]= " & $sString[0] & @CR & "string[1]: " & $sString[1] & @CR & "loop i is: " & $i)
        $sString[0] = $sString[1]
        $channel = $channel +1
        ;MsgBox(0, "", "after= " & @CR & "string[0]= " & $sString[0] & @CR & "string[1]: " & $sString[1] & @CR & "loop i is: " & $i)
        $sInsertString = 'setup channel' & $channel & ' input "' & $dir & '\'
        $aArray[$i] = _StringInsert($aArray[$i], $sInsertString, 0)

        ;insert new channel
        $newChannel = "new channel" & $channel & " broadcast enabled loop"
        _ArrayInsert($aArray, $i -1, $newChannel)

    Else
        $aArray[$i] = _StringInsert ( $aArray[$i], $sInsertString, 0)
        $aArray[$i] = $aArray[$i] & $sInsertString2
    EndIf
Next


 

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

With arrayinsert:

#include <array.au3>
$directory = "C:\"
Local $aArray[8]

$aArray[0]=10
$aArray[1]=10
$aArray[2]="Journey\Frontiers\Journey-After The Fall.mp3"
$aArray[3]="Journey\Frontiers\Journey-Ask the Lonely.mp3"
$aArray[4]="Journey\Greatest_Hits\Journey-Any Way You Want It.mp3"
$aArray[5]="Journey\Greatest_Hits\Journey-Dont Stop Believin.mp3"
$aArray[6]="Queensryche\Empire\Queensryche-01-Best I Can.mp3"
$aArray[7]="Queensryche\Empire\Queensryche-02-The Thin Line.mp3"

$bAddNewChannel1 = False ; make False if you don't want "new chanel1 broadcast enabled loop"

$iUbound = UBound($aArray)-1
$iCurrent = 0
$sLast = ""
$i = 2
While True
    $sCurrent = StringRegExpReplace($aArray[$i],"(\\.*)","")
    If $sCurrent <> $sLast Then
        $sLast = $sCurrent
        $iCurrent+=1
        $aArray[$i] = "setup channel" & $iCurrent & " input " & $directory & $aArray[$i]
        If ($iCurrent=1 And $bAddNewChannel1) Or $iCurrent>1 Then
            _ArrayInsert($aArray,$i,"new Channel" & $iCurrent & " broadcast enabled loop")
            $iUbound+=1
            $i+=1
        EndIf
    Else
        $aArray[$i] = "setup channel" & $iCurrent & " input " & $directory & $aArray[$i]
    EndIf
    $i+=1
    If $i > $iUbound Then ExitLoop
WEnd
_ArrayDisplay($aArray)
Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

charon,

Try this: ;)

#include <Array.au3>

Global $aList[] = [10, _
                   10, _
                   "Journey\Frontiers\Journey-After The Fall.mp3", _
                   "Journey\Frontiers\Journey-Ask the Lonely.mp3", _
                   "Journey\Greatest_Hits\Journey-Any Way You Want It.mp3", _
                   "Journey\Greatest_Hits\Journey-Dont Stop Believin.mp3", _
                   "Queensryche\Empire\Queensryche-01-Best I Can.mp3", _
                   "Queensryche\Empire\Queensryche-02-The Thin Line.mp3"]

_ArrayDisplay($aList, "Original", Default, 8)

$iLine = -1
$sCurrent_Band = ""
$iChannel = 0

$sString_1 = 'setup channel'
$sString_2 = 'input "C:\atest\'

While 1

    ; Next line and check still within array
    $iLine += 1
    If $iLine = UBound($aList, $UBOUND_ROWS) Then ExitLoop

    ; Skip leading lines with numbers
    If Int($aList[$iLine]) <> 0 Then ContinueLoop

    ; Split line to get band
    $aSplit = StringSplit($aList[$iLine], "\")
    If $aSplit[1] <> $sCurrent_Band Then
        ; Increase channel
        $iChannel += 1
        If $iChannel > 1 Then
            ; Insert line for channel if greater then 1
            _ArrayInsert($aList, $iLine, "new channel" & $iChannel & " broadcast enabled loop")
            ; Skip that line
            $iLine += 1
        EndIf
        ; Reset current band
        $sCurrent_Band = $aSplit[1]
    EndIf
    ; Add text to line
    $aList[$iLine] = $sString_1 & $iChannel & $sString_2 & $aList[$iLine] & '"'

WEnd

_ArrayDisplay($aList, "Amended", Default, 8)
Please ask if you have any questions. :)

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

jdelaney,

And I really did not peek at yours before posting. ;)

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

Thanks Melba23 and everyone  for all your help. I only had to change a few lines of code to get it working and I understand the code you used. The while loop and StringSplit worked a lot better. 

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