Jump to content

Simple StringRegExp to Array2D example?


Nates
 Share

Recommended Posts

I have an HTML file (string) with a bunch of lines (Snippit here: https://pastebin.com/iTZqV818 )

I then gather the ROW NUMBER of each line that starts with " &nbsp".

Next I would like to put all the results from StringRegExp on RowNum+1, into a new 2D array.

I can make multiple 1D arrays ($aPaths and $aNames)

Local $aData = FileReadToArray ( "C:\temp\spokane.html" )
  
  ; Find all rows that start with "  "
  Local $aFinds[0]
  For $i = 0 To UBound($aData)-1
    If StringLeft($aData[$i],12) = "  " Then
      _ArrayAdd($aFinds,$i)
    EndIf
  Next
  Local $iFirstLine = _ArrayMin($aFinds)
  Local $iLastLine  = _ArrayMax($aFinds) + 2
  
  ; Extract the useful info
  Local $sLine = ""
  Local $aLine = [3]
  Local $aPaths[0]
  Local $aNames[0]
  
  For $i = $iFirstLine To $iLastLine
    $aLine = StringRegExp($aData[$i+1], "<a href='(.+?)'.+(<\/a> )(.+)<BR>", 1)
    _ArrayAdd($aPaths,$aLine[0])
    _ArrayAdd($aNames,$aLine[2])
    $i = $i + 2  ; Increase to the next group
  Next
  
  _ArrayDisplay($aPaths)
  _ArrayDisplay($aNames)

But I just can't figure out how to make a single 2D array with the parts. I would have thought with perhaps $aLine[0][2], putting something in the loop like

_ArrayAdd($aNames,[$aLine[0],$aLine[2]])

would work... but no.

I've searched Google, and these forums, but the results all seemed to deal with ONE line of data into a 2D array, or multiple lines into a 1D array. I'm looking for a way to put the results [0] and [2] into two columns, in one final array. (Picture $aPaths in column 1, and $aNames in column 2)

Anyone care to give it a shot? I think I'm close, just don't know the right syntax.

Thanks!

Link to comment
Share on other sites

Or you use 4 as value for the flag parameter of stringregexp.
Then you get a Array-In-Array Structure which can be easily converted into a 2D-Array.
For example with such a function:

; #FUNCTION# ======================================================================================
; Name ..........: __ArrayAinATo2d()
; Description ...: Convert a Arrays in Array into a 2D array
; Syntax ........: __ArrayAinATo2d(ByRef $A)
; Parameters ....: $A             - the arrays in array which should be converted
; Return values .: Success: a 2D Array build from the input array
;                  Failure: False
;                     @error = 1: $A is not an 1D array
;                            = 2: $A is empty
;                            = 3: first element is not aa array
; Author ........: AspirinJunkie
; =================================================================================================
Func __ArrayAinATo2d(ByRef $A)
    If UBound($A, 0) <> 1 Then Return SetError(1, UBound($A, 0), False)
    Local $N = UBound($A)
    If $N < 1 Then Return SetError(2, $N, False)
    Local $u = UBound($A[0])
    If $u < 1 Then Return SetError(3, $u, False)

    Local $a_Ret[$N][$u]

    For $i = 0 To $N - 1
        Local $t = $A[$i]
        If UBound($t) > $u Then ReDim $a_Ret[$N][UBound($t)]
        For $j = 0 To UBound($t) - 1
            $a_Ret[$i][$j] = $t[$j]
        Next
    Next
    Return $a_Ret
EndFunc   ;==>__ArrayAinATo2d

Or you work directly with the Array-In-Array because there is not much difference in the use.

Link to comment
Share on other sites

#Include <Array.au3>

$txt = FileRead("C:\temp\spokane.html")
StringRegExpReplace($txt, '\R', @crlf)
Local $n = @extended , $aResults[$n][2], $k = 0
  
$aLine = StringRegExp($txt, "&nbsp;&nbsp.+\R.*<a href='(.+?)'.+(<\/a> )(.+)<BR>", 3)
; simpler :
; $aLine = StringRegExp($txt, "<a href='(.+?)'.+(<\/a> )(.+)<BR>", 3)

For $i = 0 To UBound($aLine)-1 step 3
   $aResults[$k][0] = $aLine[$i]
   $aResults[$k][1] = $aLine[$i+2]
   $k += 1
Next
Redim $aResults[$k][2]
_ArrayDisplay($aResults, "test")

 

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