Skip to content

Instantly share code, notes, and snippets.

@FichteFoll
Last active October 3, 2015 01:58
Show Gist options
  • Save FichteFoll/2367207 to your computer and use it in GitHub Desktop.
Save FichteFoll/2367207 to your computer and use it in GitHub Desktop.
Anime-Bilder-Rate-Spiel-Bestenliste
; This script is used to auto-generate the ranglist of correctly guessed anime.
; Usage: https://akatsuki-subs.net/forum/thread-1151.html
; Gist: https://gist.github.com/2367207
;
; (c) FichteFoll, 2012-04-12
#include <Array.au3>
#include <String.au3>
#include "LibCsv2_mod.au3"
;~ #include <_CSVLib_V1.3.au3>
Opt("MustDeclareVars", 1)
Global $nThreadID = 1151, $szCsvFile = "ABRS.csv"
Global $aszTable, $szTable, $szRanks
; ["Anime", "Löser", "Poster", "Bilder"], _ ; [4]-[n] = Post IDs für die Bilder
;~ $aszTable = _CSVReadToArray($szCsvFile)
$aszTable = _CSVReadFile($szCsvFile)
;~ _ArrayDisplay($aszTable)
$szTable = _GenerateTable($aszTable, -1, 4)
ConsoleWrite($szTable & @CRLF)
; ============================
; [0] = Position; [1] = Name; [2] = Points; [3] = Images; [4] = Solutions
Global $aRanks[1][5] = [["", "", 65532, 0, 0]]
For $i = 1 To UBound($aszTable, 1) - 1
Local $iIndex = _ArraySearch($aRanks, $aszTable[$i][2], 0, 0, 0, 0, 1, 1)
If Not @error Then
$aRanks[$iIndex][2] += 1 + $aszTable[$i][3]
$aRanks[$iIndex][3] += $aszTable[$i][3]
$aRanks[$iIndex][4] += 1
Else
$iIndex = UBound($aRanks, 1)
ReDim $aRanks[$iIndex + 1][5]
$aRanks[$iIndex][1] = $aszTable[$i][2] ; Name
$aRanks[$iIndex][2] = 1 + $aszTable[$i][3] ; Points
$aRanks[$iIndex][3] = $aszTable[$i][3] ; Images
$aRanks[$iIndex][4] = 1 ; Times
EndIf
Next
_ArraySort($aRanks, 1, 0, 0, 2)
$aRanks[0][0] = "Platz"
$aRanks[0][1] = "Name"
$aRanks[0][2] = "Punkte"
$aRanks[0][3] = "Bilder"
$aRanks[0][4] = "Gelöst"
; Sort array secondarily by Solutions and set the position, use "." as placeholder for same positionings
For $i = 1 To UBound($aRanks, 1) - 1
$aRanks[$i][0] = $i
If $aRanks[$i][2] = $aRanks[$i-1][2] Then
If $aRanks[$i][4] = $aRanks[$i-1][4] Then
$aRanks[$i][0] = _StringRepeat(".", StringLen($aRanks[$i-1][0]))
ContinueLoop
EndIf
For $m = 0 To $i-1
If $i-$m-1 < 1 Or $aRanks[$i-$m][2] <> $aRanks[$i-1][2] Then ExitLoop
If $aRanks[$i-$m][4] > $aRanks[$i-$m-1][4] Then
Local $aTemp[5]
For $j = 1 To UBound($aRanks, 2) - 1
$aTemp[$j] = $aRanks[$i-$m][$j]
$aRanks[$i-$m][$j] = $aRanks[$i-$m-1][$j]
$aRanks[$i-$m-1][$j] = $aTemp[$j]
Next
EndIf
Next
EndIf
Next
$szRanks = _GenerateTable($aRanks, -1, -1)
ConsoleWrite($szRanks & @CRLF)
; ============================
Global $szClip = StringFormat("Um ehrlich zu sein ist die Idee jetzt nicht meine Schöpfung, aber da so etwas tierisch Spaß machen kann, spielen wir hier nun auch das ABRS.\r\n\r\n" & _
"[size=large]Ein paar Regeln[/size]\r\n" & _
"Jemand postet ein Bild, es steht jedem frei, zu raten - mehrere Versuche sind erlaubt. Sollte nach einem Tag niemand die Antwort gewusst haben (vom Poster des Bildes bestätigt), darf er ein weiteres Bild posten. Wenn jemand den Anime errät, muss dieser ein neues Bild posten ...\r\n" & _
"\r\n\r\n[size=large]Liste[/size][php]\r\n")
$szClip &= $szTable
$szClip &= StringFormat("\r\n[/php]\r\n\r\n" & _
"[size=large]Rangliste[/size]\r\n" & _
"Ein gepostetes Bild und ein erratener Anime geben jeweils einen Punkt. Bei gleicher Punktzahl wird nach der Anzahl der Lösungen gewertet.\r\n\r\n" & _
"[php]\r\n")
$szClip &= $szRanks
$szClip &= StringFormat("\r\n[/php]\r\n\r\n" & _
"[size=x-small](Autogeneriert von einem [url=https://gist.github.com/2367207]AutoIt-Skript[/url].)[/size]\r\n\r\n\r\n" & _
"Ich fange mal ""einfach"" an:\r\n" & _
"[url=http://i.imgur.com/Z2S3v.jpg][img]http://i.imgur.com/Z2S3vl.jpg[/img][/url]")
; == Teh Output
ClipPut($szClip)
; =========================================================
Func _GenerateTable(ByRef $aArray, $1dDim = -1, $2dDim = -1)
If Not IsArray($aArray) Or UBound($aArray, 0) <> 2 Then Return SetError(1, 0, "")
Local $aiElmts[2] = [UBound($aArray, 1), UBound($aArray, 2)]
If $1dDim > 0 Then $aiElmts[0] = $1dDim
If $2dDim > 0 Then $aiElmts[1] = $2dDim
Local $aiLenMax[$aiElmts[1]], _
$acFlag[$aiElmts[1]], _
$sOutput, $sExec, _
$sLine, $sSepLine
; MaxLen & Align
For $i = 0 To $aiElmts[0] - 1
For $j = 0 To $aiElmts[1] - 1
;~ If $i = 0 And $aArray[0][$j] = "" Then $aArray[0][$j] = "Bild " & $j - 3 ; Fill empty headers
$aiLenMax[$j] = __Max(StringLen(String($aArray[$i][$j])), $aiLenMax[$j])
; Set alignment only if in the second line (no header) and contents are not multiples of "." (they are used in the rankings)
If $i > 0 And Not StringIsInt($aArray[$i][$j]) And $aArray[$i][$j] <> "" And Not StringRegExp($aArray[$i][$j], "^\.+$") Then $acFlag[$j] = '-'
Next
Next
; Define $sSepLine
$sLine = '+' ; Cross
For $i = 0 To $aiElmts[1] - 1
$sLine &= '-' ; Blank
$sLine &= _StringRepeat('-', $aiLenMax[$i])
$sLine &= '-+' ; Blank & Cross
Next
$sSepLine = $sLine
; Start
$sOutput = $sSepLine
; Header
$sExec = "StringFormat('" ; Using variable StringFormat as it supports right alignment
For $j = 0 To $aiElmts[1] - 1
$sExec &= "| %" & $acFlag[$j] & $aiLenMax[$j] & "s "
Next
$sExec &= "|'"
For $j = 0 To $aiElmts[1] - 1
$sExec &= ", '" & StringReplace(String($aArray[0][$j]), "'", "\'") & "'"
Next
$sExec &= ")"
$sLine = Execute($sExec)
$sOutput &= @CRLF & $sLine
; Seperator
$sOutput &= @CRLF & $sSepLine
; Lines
For $i = 1 To $aiElmts[0] - 1
$sExec = "StringFormat('"
For $j = 0 To $aiElmts[1] - 1
If $j >= 4 And $aArray[$i][$j] <> '' And StringRegExp($aArray[0][$j], "^Bild \d+$") Then
; Insert [url] tags for the post number
$aArray[$i][$j] = StringFormat("[url=http://akatsuki-subs.net/forum/thread-%d-post-%d.html#pid%d]%s[/url]", $nThreadID, $aArray[$i][$j], $aArray[$i][$j], $aArray[0][$j])
$sExec &= "| %-s "
Else
$sExec &= "| %" & $acFlag[$j] & $aiLenMax[$j] & "s "
EndIf
Next
$sExec &= "|'"
For $j = 0 To $aiElmts[1] - 1
$sExec &= ", '" & StringReplace(String($aArray[$i][$j]), "'", "\'") & "'"
Next
$sExec &= ")"
$sLine = Execute($sExec)
$sOutput &= @CRLF & $sLine
Next
; Footer
$sOutput &= @CRLF & $sSepLine
Return $sOutput
EndFunc ;==>_GenerateTable
; _Max from Math.au3 checks the parameters for IsNumber() -
; which is not the case here and does not behave as wanted.
Func __Max($nNum1, $nNum2)
If $nNum1 > $nNum2 Then Return $nNum1
Return $nNum2
EndFunc ;==>__Max
;~ Func _CSVReadToArray($szFilePath, $cDelimiter = ",", $cEnclose = '"')
;~ Local $hFile, $szContents, $aRecords, $RegEx
;~ Local $iUbound, $iUbound2 = 1, $iUbound2Max = 1, $iUboundLine
;~ If Not FileExists($szFilePath) Then Return SetError(1)
;~ If StringLen($cDelimiter) <> 1 Or StringLen($cEnclose) <> 1 Then Return SetError(2)
;~ ; Read file
;~ ;$szContents = FileRead($hFile)
;~ ;$szContents = StringRegExpReplace($szContents, "\r\n|\r(?!=\n)", @LF)
;~ ; Read file
;~ $aRecords = _CSVFileReadRecords($szFilePath)
;~ If @error Then Return SetError(3)
;~ $iUbound = $aRecords[0]
;~ ; Parse lines
;~ Local $aReturn[$iUbound][$iUbound2], $aFields
;~ Local $iDelimiter, $iEnclose, $iNextDelimiter, $iNextEnclose
;~ For $i = 0 To $iUbound - 1
;~ $aFields = _CSVRecordGetFields($aRecords[$i+1], ',', '"')
;~ ; resize array if necessary
;~ $iUbound2 = _Max($iUbound2, $aFields[0])
;~ If $iUbound2 > $iUbound2Max Then
;~ $iUbound2Max = $iUbound2*2
;~ ReDim $aReturn[$iUbound][$iUbound2Max]
;~ EndIf
;~ ; fill array with line
;~ For $j = 0 To $aFields[0] - 1
;~ $aReturn[$i][$j] = $aFields[$j+1]
;~ Next
;~ Next
;~ ReDim $aReturn[$iUbound][$iUbound2]
;~ Return $aReturn
;~ EndFunc
We can make this file beautiful and searchable if this error is corrected: It looks like row 2 should actually have 4 columns, instead of 5 in line 1.
Anime,Löser,Poster,Bilder
Koe de Oshigoto!,Quester,FichteFoll,1,7140
Bakemonogatari,FichteFoll,Quester,1,7150
Gosick,Kisuke,FichteFoll,1,7151
Oh! My Goddess,saeko,Kisuke,1,7155
Rosario to Vampire,Schaaf,saeko,1,7159
Fractale,revil,Schaaf,1,7162
Kiba,Quester,revil,1,7165
Hand Maid Mai,revil,Quester,1,7168
Wangan Midnight,Schaaf,revil,2,7172,7172
Macross F,revil,Schaaf,1,7181
Eyeshield 21,Schaaf,revil,1,7184
Fairy Tail,revil,Schaaf,1,7187
The World God Only Knows,klose91,revil,1,7190
Kimi ni Todoke,revil,klose91,1,7193
Code Geass,klose91,revil,1,7196
IS: Infinite Stratos,revil,klose91,1,7201
Kyou Kara Maou!,klose91,revil,2,7204,7204
Hanbun no Tsuki ga Noboru Sora,revil,klose91,1,7210
ef - a tale of melodies.,FichteFoll,revil,2,7213
Soukou no Strain,Quester,FichteFoll,2,7218,7226
Sayonara Zetsubou Sensei,FichteFoll,Quester,2,7229,7229
Toaru Majutsu no Index,klose91,FichteFoll,1,7237
Boku wa Imouto ni Koi wo Suru,Quester,klose91,1,7240
Suzumiya Haruhi no Shoushitsu,klose91,Quester,2,7243,7248
NHK ni Youkoso!,Quester,klose91,1,7273
Hidamari Sketch x ,revil,Quester,2,7280,7280
Suzuka,Quester,revil,1,7311
Baccano!,revil,Quester,2,7320,7351
Sunabouzu,Quester,revil,3,7366,7386,7386
Toaru Majutsu no Index II,FichteFoll,Quester,1,7416
Kaichou wa Maid-sama!,klose91,FichteFoll,1,7419
D.N.Angel,Quester,klose91,1,7424
Pani Poni Dash!,FichteFoll,Quester,1,7450
Angel Beats!,Ramses87,FichteFoll,1,7455
Paradise Kiss,FichteFoll,Ramses87,1,7470
Elfen Lied,Quester,FichteFoll,2,7480,7480
Kämpfer,Kuragari,Quester,1,7486
IS: Infinite Stratos,FichteFoll,Kuragari,1,7490
Shiki,Kuragari,FichteFoll,1,7496
Guardian of the Spirit,revil,Kuragari,6,7501,7501,7501,7501,7501,7501
Zero no Tsukaima,Kuragari,revil,1,7562
Blue Submarine No. 6,Rasetsu,Kuragari,5,7565,7565,7565,7565,7565
Pita-Ten,Kuragari,Rasetsu,3,7635,7635,7635
Angel Sanctuary,revil,Kuragari,4,7720,7720,7720,7720
Kekkaishi,TJJJ85,revil,6,7781,7781,7781,7781,7781,7781
R.O.D -THE TV-,revil,TJJJ85,1,7798
Tonagura!,TJJJ85,revil,1,7801
Monochrome Factor,Kuragari,TJJJ85,1,7805
Beelzebub,klose91,Kuragari,3,7811,7811,7811
Houkago no Pleiades,Kuragari,klose91,1,7840
Witch Hunter Robin,Quester,Kuragari,1,7968
Jungle wa Itsumo Hare nochi Guu,klose91,Quester,4,8074,8176,8206,8206
Guardian Hearts,revil,klose91,3,8534,8534,8594
5 Centimeter per Second,Quester,revil,1,8629
Hyakko,saeko,Quester,1,8640
Dog Days,klose91,saeko,1,8653
Morita-san wa Mukuchi,FichteFoll,klose91,5,8655,8655,8673,8673,8777
Yumekui Merry,klose91,FichteFoll,2,8780,8780
Ano [...] Hana [...],FichteFoll,klose91,1,8795
Higurashi no Naku Koro ni Kai,Rasetsu,FichteFoll,4,8800,8800,8832,8832
Utawarerumono,klose91,Rasetsu,1,8865
Honto ni Atta! Reibai sensei!,Rasetsu,klose91,1,8905
Mirai Nikki (OVA),FichteFoll,Rasetsu,1,8865
Highschool of the Dead,saeko,FichteFoll,2,8932,8945
Hyakka Ryouran Samurai Girls,klose91,saeko,1,8865
Mahou Sensei Negima,saeko,klose91,1,8953
Code Geass,revil,saeko,1,8955
True Tears,Quester,revil,1,8971
Oreimo,Kisuke,Quester,1,9199
Ichiban Ushiro no Daimaou ,Quester,Kisuke,1,9206
Itsuka Tenma no Kuro Usagi,FichteFoll,Quester,1,9230
Touhou Gensou Mangekyo,Quester,FichteFoll,2,9242,9249
Shiki,FichteFoll,Quester,2,9272,9388
Canaan,Quester,FichteFoll,2,9392,9446
Shinryaku!? Ika Musume,FichteFoll,Quester,3,9479,9479,9959
Mayo Chiki!,_nerosan,FichteFoll,1,9978
Spice and Wolf,Quester,_nerosan,1,10061
Hen Semi,_nerosan,Quester,4,10064,10158,10180,10240
Legend of the Legendary Heroes,Quester,_nerosan,1,10246
Kuroshitsuji,klose91,Quester,4,10383,10432,10466,10514
Yumeiro Pâtissière,FichteFoll,klose91,3,10525,10525,10548
Deadman Wonderland,_nerosan,FichteFoll,2,10553,10561
Bamboo Blade,Quester,_nerosan,2,10565,10568
30-sai no Hoken Taiiku,_nerosan,Quester,1,10589
Nurarihyon no Mago,klose91,_nerosan,2,10592,10605
"Kyou, Koi wo Hajimemasu",Quester,klose91,1,10612
Welcome to the N.H.K.,skyleo,Quester,1,10741
Ragnarok the Animation,revil,skyleo,2,10744,10756
D.Gray-man,FichteFoll,revil,1,10759
Kamisama Dolls,Quester,FichteFoll,1,10763
C: [...],klose91,Quester,1,10773
Houhokekyo Tonari no Yamada-kun,Asmoraiden,klose91,1,10842
Noir,Quester,Asmoraiden,1,10846
Darker than Black,Mewbatz,Quester,1,10863
Deadman Wonderland,Quester,Mewbatz,1,10872
Bokusatsu Tenshi Dokuro-chan,skyleo,Quester,1,10925
Dantalian no Shoka,revil,skyleo,3,10928,10933,10936
Suzuka,Kuragari,revil,1,10941
Onegai Teacher,Quester,Kuragari,1,11125
Zetman,FichteFoll,Quester,1,11136
Seitokai Yakuindomo,klose91,FichteFoll,3,11141,11154,11157
Aishiteruze Baby,_nerosan,klose91,2,11170,11170
Steins;Gate,Quester,FichteFoll,2,11279,11288
ChäoS;HEAd,saeko,Quester,1,11325
Princess Lover,klose91,saeko,1,11331
Natsuyuki Rendezvous,Quester,klose91,4,11334,11337,11337,11337
Katanagatari,saeko,Quester,2,11365,11370
Ikkitousen,klose91,saeko,3,11373,11373,11373
"Dakara Boku wa, H ga Dekinai",Quester,klose91,4,11388,11388,11392,11400
Black Lagoon,klose91,Quester,6,11403,11421,11428,11459,11479,11490
Sword Art Online,_nerosan,klose91,1,11494
Da Capo Second Season,...,_nerosan,1,11593
Hetalia World Series,pybcu,skyleo,1,11707
Puni Puni Poemi,Quester,pybcu,1,11706
Nazo no Kanojo X,skyleo,Quester,1,11711
Kyoukaisenjou no Horizon,Quester,skyleo,1,11714
Nichijou,FichtFoll,Quester,2,11717,11723
Guilty Crown,skyleo,FichtFoll,2,11732,11743
Bungaku Shoujo: Memoire,Kuragari,skyleo,3,11746,11765,11772
The Sky Crawlers,klose91,Kuragari,2,11787,11794
Hotarubi no Mori e,Kisuke,Kuragari,6,11797,11797,11797,11797,11797,11797
Shakugan no Shana,klose91,Kisuke,1,11802
High School! Kimengumi,Kuragari,klose91,14,11805,11805,11805,11805,11805,11805,11805,11805,11805,11805,11805,11805,11805,11805
Dansai Bunri no Crime Edge,klose91,Kisuke,3,11878,11878,11878
Shingeki no Kyojin,FichteFoll,klose91,1,11885
Haiyore! Nyarlko-san?,klose91,FichteFoll,1,11888
...,...,klose91,1,11892
; PREG-based CSV file parser.
; Copyright 2007, Ed Fletcher
; Modifications by FichteFoll, (02-2012):
; - added $cEnclose parameter for defining the enclosing character (default: ")
; - have _CSVGetColumn and _CSVGetRow use Const ByRef for the first param to save ressources
; - allow _CSVGetColumn and _CSVGetRow to use negative numbers as index (parsing backwards)
; and add some error handling for invalid parameters
; - fix use of "Die" as debug function since this is not defined
; - modified _CSVTest to accept the same parameters as _CSVReadFile
;
; http://www.autoitscript.com/forum/topic/50680-simple-quick-csv-parser/page__p__961109#entry961109
; #AutoIt3Wrapper_AU3Check_Parameters=-d -w 3 -w 4 -w 5 -w 6
#include-once
;===============================================================================
;
; Description: Reads a CSV file into a 2D array
; Parameter(s): $sPath - Path to the CSV file to read
; $cSeparator - Separator character, default is comma (,)
; $cEnclose - Character used in enclosings, default is "
; Requirement(s): None
; Return Value(s): On Success - 2D CSV array
; On Failure - 0 and Set
; @ERROR to: 1 - File not found/openable
; 2 - File read error
; 3 - CSV format error
; Author(s): Ed Fletcher
; Note(s): Pattern based on work by Jeffrey E. F. Friedl in
; "Mastering Regular Expressions, 2nd Edition"
;===============================================================================
Func _CSVReadFile( $path, $separator = ',', $enclose = '"' )
;; open the file and read the entire CSV dataset into one string.
Local $hFile = FileOpen( $path, 0 )
If $hFile == -1 Then
SetError( 1 )
Return 0
EndIf
Local $sRawData = FileRead( $hFile )
If @error > 0 Then
FileClose( $hFile )
SetError( 2 )
Return 0
EndIf
FileClose( $hFile )
;; parse the string into an array of matched fields
Local $pattern = '(?m)' ; multi-line search mode
$pattern &= '\G(?:^|[' & $separator & '])' ; start of line or start of field
$pattern &= '(?:' ; one of two options:
$pattern &= $enclose ; a field starting with at double quote
$pattern &= StringFormat('([^%s]*+(?:%s%s[^%s]*+)*+)', $enclose, $enclose, $enclose, $enclose)
; ; (quote-pairs and any non-quote chars)
$pattern &= $enclose ; a double quote ending the field
$pattern &= '(\r?\n?)' ; (any sort of line ending here?)
$pattern &= '|' ; or:
$pattern &= '([^"' & $separator & '\r\n]*+)' ; (a simple CSV field, no quotes or commas)
$pattern &= '(\r?\n?)' ; (any sort of line ending here?)
$pattern &= ')' ; note that we should have 4 captures per CSV element
Local $aRawData = StringRegExp( $sRawData, $pattern, 4 )
If @error <> 0 Then
SetError( 3 )
Return 0
EndIf
$sRawData = '' ; we're done with this, and it might be large
; $aRawData is a 1D array containing every field in the CSV file. Each element
; in $aRawData is an array of 5 strings, like so:
; 0 - all of the characters consumed while matching this field
; 1 - field contents, if the field was double quoted
; 2 - a line ending, if the field was double quoted and this is the end of the line
; 3 - field contents, if the field was *not* double quoted
; 4 - a line ending, if the field was *not* double quoted and this is the end of the line
;; pass through the results once to determine the number of rows and the max number of columns
Local $i, $aMatch
Local $colCount = 0, $maxCols = 0
Local $rowCount = 0
For $i=0 To UBound($aRawData)-1
$aMatch = $aRawData[$i]
If $colCount == 0 Then
$rowCount += 1 ; we're looking at the first field on the current row
EndIf
$colCount += 1
If $colCount > $maxCols Then
$maxCols = $colCount ; longest row so far...
EndIf
If $aMatch[2] <> '' OR (UBound($aMatch) > 3 AND $aMatch[4] <> '') Then
$colCount = 0 ; row complete, we might start a new one
EndIf
Next
;; we now know how large to make our 2D output array
Local $aCsvData[$rowCount][$maxCols]
;; finally, populate our output array
Local $row = 0, $col = 0
For $i=0 To UBound($aRawData)-1
$aMatch = $aRawData[$i]
If UBound($aMatch) > 3 AND $aMatch[3] <> '' Then
; It was a simple field, no processing required
$aCsvData[$row][$col] = $aMatch[3]
Else
; It was a quoted value, so take care of embedded double quotes
$aCsvData[$row][$col] = StringReplace($aMatch[1], '""', '"')
EndIf
$col += 1
; now look for a line ending that ends the current data row
If $aMatch[2] <> '' OR (UBound($aMatch) > 3 AND $aMatch[4] <> '') Then
$row += 1
$col = 0
EndIf
Next
Return $aCsvData
EndFunc
;===============================================================================
;
; Description: Pulls a single column out of a 2D array
; Parameter(s): $aCSV - 2D array to work with; Const ByRef
; $colNum - Column index, 0-based;
; Negative numbers for backwards parsing are allowed
; Requirement(s): None
; Return Value(s): On Success - An array of columnar data
; On Failure - 0 and Set
; @ERROR to: 1 - Dimension mismatch; only 2D arrays!
; 2 - $colNum is invalid
; 3 - $colNum exceeds column count
; Note(s):
;
;===============================================================================
Func _CSVGetColumn( Const ByRef $aCSV, $colNum )
; test array dimensions
If UBound($aCSV, 0) <> 2 Then
SetError( 1 )
Return 0
EndIf
; test second parameter for validity
$colNum = Int($colNum) ; cast strings
If Not IsInt($colNum) Then
SetError( 2 )
Return 0
EndIf
Local $aBounds[2] = [UBound($aCSV, 1), UBound($aCSV, 2)]
; test second parameter for validity (2)
If $colNum < 0 Then $colNum = $aBounds[1] + $colNum
If $colNum < 0 Or $colNum > ($aBounds[1] - 1) Then
SetError( 3 )
Return 0
EndIf
; start with defining the return array
Local $aColumn[$aBounds[0]]
Local $i
For $i=0 To $aBounds[0]-1
$aColumn[$i] = $aCSV[$i][$colNum]
Next
Return $aColumn
EndFunc
;===============================================================================
;
; Description: Pulls a single row out of a 2D array
; Parameter(s): $aCSV - 2D array to work with; Const ByRef
; $rowNum - Row index, 0-based;
; Negative numbers for backwards parsing are allowed
; Requirement(s): None
; Return Value(s): On Success - An array of row data
; On Failure - 0 and Set
; @ERROR to: 1 - Dimension mismatch; only 2D arrays!
; 2 - $rowNum is invalid
; 3 - $rowNum exceeds column count
; Note(s):
;
;===============================================================================
Func _CSVGetRow( Const ByRef $aCSV, $rowNum )
; test array dimensions
If UBound($aCSV, 0) <> 2 Then
SetError( 1 )
Return 0
EndIf
; test second parameter for validity
$colNum = Int($rowNum) ; cast strings
If Not IsInt($rowNum) Then
SetError( 2 )
Return 0
EndIf
Local $aBounds[2] = [UBound($aCSV, 1), UBound($aCSV, 2)]
; test second parameter for validity (2)
If $rowNum < 0 Then $rowNum = $aBounds[0] + $rowNum
If $rowNum < 0 Or $rowNum > ($aBounds[0] - 1) Then
SetError( 3 )
Return 0
EndIf
; start with defining the return array
Local $aRow[$aBounds[1]]
Local $i
For $i=0 To $aBounds[1]-1
$aRow[$i] = $aCSV[$rowNum][$i]
Next
Return $aRow
EndFunc
;===============================================================================
;
; Description: Test function for _CSVReadFile()
; Parameter(s): $sPath - Path to the file to read, default is 'test.csv'
; $cSeparator - Separator character, default is comma (,)
; $cEnclose - Character used in enclosings, default is "
; Requirement(s): None
; Return Value(s): None
; Note(s): Dumps out array data via ConsoleWrite()
;
;===============================================================================
Func _CSVTest( $file = "test.csv", $separator = ',', $enclose = '"' )
Local $output = _CSVReadFile( $file, $separator, $enclose )
If @error <> 0 Then
ConsoleWriteError( "Error " & @error & " reading file [" & $file & "]" & @CR )
Exit
EndIf
Local $i, $j
For $i=0 To UBound($output)-1
For $j=0 To UBound($output, 2)-1
ConsoleWrite('[' & $output[$i][$j] & ']')
Next
ConsoleWrite(@CR)
Next
EndFunc
;~ If @ScriptName == 'LibCsv2_mod.au3' Then
;~ _CSVTest("ABRS.csv")
;~ EndIf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment