Jump to content

Insert column with row number to existing array script


Go to solution Solved by Nine,

Recommended Posts

Hi I am using an existing script to extract a 2D array (bottom of post)

when using "_ArrayDisplay" I can get a first column containing the row number; 

_ArrayDisplay($playlist, "2D display", "1:|:0")

image.png.2535dc8514ea0006fa771f9c9877b792.png

however, if I want to send the output to file with "_FileWriteFromArray", the "row number" column is not an option.

_FileWriteFromArray($sFilePath, $playlist, 1, Default, " , ")

looking for guidance on how to add the "row number" as a first column to the script providing the array (below),  some way of transferring _arraydisplay results to file or change the _FileWriteFromArray call to generate the desired output.

;===============================================================================
;
; Function Name:           _Winamp_GetPlayListToArray()
;
; Function Description:    Get Current Playlist to array.
;
; Parameter(s):            $h_Winamp_Wnd - [Optional] Winamp window handle to use
;                                           (by default the handle is taken from $sWINAMP_CLASS).
;
; Requirement(s):          None.
;
; Return Value(s):         On Success -  Returns 2 dimensional array $aRet_Array with the PlayList entries, where:
;                                                                   [0][0] = Array element of current song (in $aRet_Array).
;                                                                   [n][0] = Song Title.
;                                                                   [n][1] = Song File Path.
;
;                          On Failure -  Return 0 and set @error to:
;                                                                   1 - Unable to find Winamp window, probably winamp isn't runing.
;                                                                   2 - SendMessage fail.
;                                                                   3 - Can not access to winamp playlist memory.
;                                                                   4 - Error to Open user32.dll.
;
; Author(s):               weaponx (mod. by G.Sandler)
;
;=====================================================================
Func _Winamp_GetPlayListToArray($h_Winamp_Wnd = 0)
    Local $aRet_Array[1][1]
    Local $hWinamp, $iWinamp_PID, $iNull, $pAddress_Title, $pAddress_FPath, $hUser32Dll, $iNumTracks, $aCurrent_Track

    $hWinamp = WinGetHandle($sWINAMP_CLASS)
    If IsHWnd($h_Winamp_Wnd) Then $hWinamp = $h_Winamp_Wnd

    If Not IsHWnd($hWinamp) Then Return SetError(1, 0, 0)

    $iWinamp_PID = WinGetProcess($hWinamp)

    ;Return number of tracks in playlist
    $iNumTracks = DllCall("user32.dll", "int", "SendMessage", "hwnd", $hWinamp, "int", _
        $WM_WA_IPC, "int", 0, "int", $IPC_GETLISTLENGTH)
    If @error Then Return SetError(2, 0, 0)

    $iNumTracks = $iNumTracks[0]

    $aRet_Array[0][0] = $iNumTracks
    ReDim $aRet_Array[$iNumTracks + 1][2]

    ;Return current playlist position (zero based)
    $aCurrent_Track = DllCall("user32.dll", "int", "SendMessage", _
        "hwnd", $hWinamp, "int", $WM_WA_IPC, "int", 0, "int", $IPC_GETLISTPOS)

    If Not @error Then $aRet_Array[0][0] = $aCurrent_Track[0] + 1

    ;IMPORTANT - First song will be skipped without this
    $iNull = __API_GetInfoByAddress(0, 0)
    If @error Then Return SetError(3, 0, 0)

    $hUser32Dll = DllOpen("user32.dll")
    If $hUser32Dll = -1 Then Return SetError(4, 0, 0)

    For $i = 0 To $iNumTracks - 1
        $pAddress_Title = 0
        $pAddress_FPath = 0

        ;Return memory address (pointer) of track title
        $pAddress_Title = DllCall($hUser32Dll, "int", "SendMessage", _
            "hwnd", $hWinamp, "int", $WM_WA_IPC, "int", $i, "int", $IPC_GET_PLAYLISTTITLE)
        If @error Then ContinueLoop

        $aRet_Array[$i + 1][0] = __API_GetInfoByAddress($pAddress_Title[0], $iWinamp_PID)
        If @error Then Return SetError(3, 0, 0)

        ;Return memory address (pointer) of track file path
        $pAddress_FPath = DllCall($hUser32Dll, "int", "SendMessage", _
            "hwnd", $hWinamp, "int", $WM_WA_IPC, "int", $i, "int", $IPC_GETPLAYLISTFILEW)
        If @error Then ContinueLoop

        $aRet_Array[$i + 1][1] = __API_GetInfoByAddress($pAddress_FPath[0], $iWinamp_PID)
        If @error Then Return SetError(3, 0, 0)
    Next

    DllClose($hUser32Dll)

    Return $aRet_Array
EndFunc   ;==>_Winamp_GetPlayListToArray

Thanks for your help!

Edited by Homeseer666
Link to comment
Share on other sites

#include <Array.au3>

Example()

;----------------------------------------------------------------------------------------
Func Example()
    Local $Array[][] = [ _
            ["Month", "Bears", "Dolphins", "Whales"], _
            ["jan", 8, 150, 80], _
            ["feb", 54, 77, 54], _
            ["mar", 93, 32, 10], _
            ["apr", 116, 11, 76], _
            ["may", 137, 6, 93], _
            ["jun", 184, 1, 72]]

    _ArrayDisplay($Array)

    $iColCnt = UBound($Array, 2) ; last col
;~  $iColCnt = 0 ; first col

    _ArrayColInsert($Array, $iColCnt)

    For $i = 1 To UBound($Array, 1) - 1
        $Array[$i][$iColCnt] = $i
    Next

    _ArrayDisplay($Array)
EndFunc   ;==>Example

 

I know that I know nothing

Link to comment
Share on other sites

  • Solution

The fastest way is to FileWrite what you want using file handle :

#include <File.au3>

Local $Array[][] = [ _
    ["Month", "Bears", "Dolphins", "Whales"], _
    ["jan", 8, 150, 80], _
    ["feb", 54, 77, 54], _
    ["mar", 93, 32, 10], _
    ["apr", 116, 11, 76], _
    ["may", 137, 6, 93], _
    ["jun", 184, 1, 72]]

Example($Array)

;----------------------------------------------------------------------------------------
Func Example(ByRef $aList)
  Local $hFile = FileOpen("Test.txt", $FO_OVERWRITE)
  For $i = 1 To UBound($aList) - 1
    FileWrite($hFile, $i)
    For $j = 0 To UBound($aList, 2) - 1
      FileWrite($hFile, "|" & $aList[$i][$j])
    Next
    FileWrite($hFile, @CRLF)
  Next
  FileClose($hFile)
EndFunc   ;==>Example

 

Link to comment
Share on other sites

Posted (edited)

Thank you,

the array can grow to more than 10000 lines so I tested/implemented your solution. It does perform very well.

Again thanks to both of you 👍 

 

Func Array2file(ByRef $aList)
      Local $sFilePath = "z:\www\winamp\winamp_playlist.txt"
      Local $hFile = FileOpen($sFilePath, $FO_OVERWRITE)
  For $i = 1 To UBound($aList) - 1
    FileWrite($hFile, $i)
;    For $j = 0 To UBound($aList, 2) - 1 # maximum columns calculation
     For $j = 0 To 0 ; # column 0 to 0 (first column only)
    FileWrite($hFile, " | " & $aList[$i][$j] & " <br>"  )
    Next
    FileWrite($hFile, @CRLF)
  Next
  FileClose($hFile)
EndFunc   ;==>Array2file

Array2file($playlist)

 

 

Edited by Homeseer666
Link to comment
Share on other sites

@Homeseer666  Just remove the $j loop if you only care with one column, hardcode 0 in place of $j (save you a bit of time)

Moreover, you could put all the things you want to FileWrite into a single statement...

Edited by Nine
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...