Jump to content

StringRegExp in 2 Array Colums?


Recommended Posts

$Array = StringRegExp($File, '<tr class="reportrow" data-href="(\d+)"><td>.+</td><td>.+</td><td>.+</td><td>(.+)</td></tr>', 3)
_ArrayDisplay($Array,"")

There is a (d+) and a (.+). 

ArrayDisplay show it me all in 1 Col.

1|int

2|word

3|int

4|word

5|int

6|word

But I want it like this way

1|int|word

2|int|word

3|int|word

How will it possible?

Edited by kelevr
Link to comment
Share on other sites

You'll have to create and fill a new 2D array since StringRegExp only produces 1D arrays.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Here are four methods you can try.
The first two methods access the array data as if in an imaginary 2D array, but staying in the format of a 1D array.
The last two methods create a 2D array and transfers the data from the 1D array.

#include <Array.au3>

Local $aArray[6] = ["1|int", "2|word", "3|int", "4|word", "5|int", "6|word"]
Local $iCol = 2

; Method 1
ConsoleWrite(@LF & "----- 1 For...Next Loop 1D array -----------------------" & @LF)
Local $sData = "Col 0" & @TAB & "Col 1" & @LF
For $i = 0 To UBound($aArray) - 1
    $sData &= $aArray[$i] & (Mod($i + 1, $iCol) ? @TAB : @CRLF)
Next
ConsoleWrite(StringTrimRight($sData, 2) & @LF) ; Remove trailing CR and LF from $sData.

; Method 2
ConsoleWrite(@LF & "----- 2 For...Next Loops 1D array -----------------------" & @LF)
Local $sData2 = "Col 0" & @TAB & "Col 1" & @LF
For $i = 0 To UBound($aArray) - 1 Step $iCol ; $aArray[$i] is equivalent of accessing the data in the 1st column, "Col 0" of each row of a 2D array.
    For $j = $i To $i + $iCol - 1
        $sData2 &= $aArray[$j] & @TAB
    Next
    $sData2 = StringTrimRight($sData2, 1) & @CRLF ; Remove trailing Tab
Next
ConsoleWrite(StringTrimRight($sData2, 2) & @LF) ; Remove trailing CR and LF.
_ArrayDisplay($aArray, "1D Array")

; Method 3
ConsoleWrite(@LF & "----- 1 For...Next Loop 2D array -----------------------" & @LF)
Dim $aArray2D[UBound($aArray) / $iCol][$iCol]
For $i = 0 To UBound($aArray) - 1
    $aArray2D[Int($i / $iCol)][Mod($i, $iCol)] = $aArray[$i]
    ConsoleWrite("$aArray2D[" & Int($i / $iCol) & "][" & Mod($i, $iCol) & "] = $aArray[" & $i & "]" & @LF)
Next
_ArrayDisplay($aArray2D, "1 For...Next Loop 2D array")

; Method 4
ConsoleWrite(@LF & "----- 2 For...Next Loops 2D array -----------------------" & @LF)
Dim $aArray2Db[UBound($aArray) / $iCol][$iCol]
For $i = 0 To UBound($aArray2Db, 1) - 1
    For $j = 0 To UBound($aArray2Db, 2) - 1
        $aArray2Db[$i][$j] = $aArray[$i * $iCol + $j] ; $iCol = UBound($aArray2Db,2)
        ConsoleWrite("$aArray2Db[" & $i & "][" & $j & "] = $aArray[" & $i * $iCol + $j & "]" & @LF); $iCol = UBound($aArray2Db,2)
    Next
Next
_ArrayDisplay($aArray2Db, "2 For...Next Loops 2D array")

#cs
@ console output:-
----- 1 For...Next Loop 1D array -----------------------
Col 0    Col 1
1|int    2|word
3|int    4|word
5|int    6|word

----- 2 For...Next Loops 1D array -----------------------
Col 0    Col 1
1|int    2|word
3|int    4|word
5|int    6|word

----- 1 For...Next Loop 2D array -----------------------
$aArray2D[0][0] = $aArray[0]
$aArray2D[0][1] = $aArray[1]
$aArray2D[1][0] = $aArray[2]
$aArray2D[1][1] = $aArray[3]
$aArray2D[2][0] = $aArray[4]
$aArray2D[2][1] = $aArray[5]

----- 2 For...Next Loops 2D array -----------------------
$aArray2Db[0][0] = $aArray[0]
$aArray2Db[0][1] = $aArray[1]
$aArray2Db[1][0] = $aArray[2]
$aArray2Db[1][1] = $aArray[3]
$aArray2Db[2][0] = $aArray[4]
$aArray2Db[2][1] = $aArray[5]
#ce
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...