ioa747 Posted May 8 Posted May 8 (edited) _ArrayToMdTable Converts an array to a markdown table format | Item | In Stock | Price | | :---------------- | :------: | ----: | | Python Hat | True | 23.99 | | SQL Hat | True | 23.99 | | Codecademy Tee | False | 19.99 | | Codecademy Hoodie | False | 42.99 | @SOLVE-SMART I did it on the occasion of your suggestion. I modified it to better suit the requirements of Markdown, and to be more readable. expandcollapse popup; https://www.autoitscript.com/forum/topic/212883-_arraytomdtable ;---------------------------------------------------------------------------------------- ; Title...........: _ArrayToMdTable ; Description.....: Converts an array to a markdown table format. ; AutoIt Version..: 3.3.16.1 Author: ioa747 Script Version: 1.0 ; Note............: Testet in Win10 22H2 ;---------------------------------------------------------------------------------------- #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 #include <Array.au3> #include <String.au3> Example1() ; Example 2D array to table Example2() ; Example 1D array to table ;--------------------------------------------------------------------------------------- Func Example1() ; Example 2D array to table Local $aData[5][3] = [["Item", "In Stock", "Price"], _ ["Python Hat", "True", "23.99"], _ ["SQL Hat", "True", "23.99"], _ ["Codecademy Tee", "False", "19.99"], _ ["Codecademy Hoodie", "False", "42.99"]] Local $sTable = _ArrayToMdTable($aData, "L,C,R") ConsoleWrite($sTable) EndFunc ;==>Example1 ;--------------------------------------------------------------------------------------- Func Example2() ; Example 1D array to table Local $sMonth = "Months, January, February, March, April, May, June, July, August, September, October, November, December" Local $aMonth = StringSplit($sMonth, ", ", 3) $sMonth = _ArrayToMdTable($aMonth, "C") ConsoleWrite($sMonth) EndFunc ;==>Example2 ; #FUNCTION# -------------------------------------------------------------------------------------------------------------------- ; Name...........: _ArrayToMDtable ; Description....: Converts an array to a Markdown table format. ; Syntax.........: _ArrayToMdTable( $aArray [, $sAlign = "" ] ) ; Parameters.....: $aArray - The array to be converted. ; $sAlign - [optional] Alignment options for each column (e.g., "L,R,C"). (Default is "" (left-aligned)) ; Return values..: The formatted table as a string. ; Author ........: ioa747 ; Notes .........: This function takes a array and converts it into a Markdown table. ; It calculates the width of each column based on the longest string in that column. ; Link ..........: ; Dependencies...: __FormatCell() ;------------------------------------------------------------------------------------------------------------------------------------ Func _ArrayToMdTable($aArray, $sAlign = "") Local $sTable = "" Local $b2D = (UBound($aArray, 0) = 1 ? False : True) ; if $aArray = 1D => force 2D (Insert extra coloumn) If Not $b2D Then _ArrayColInsert($aArray, 1) Local $iRowCount = UBound($aArray, 1) Local $iColCount = UBound($aArray, 2) Local $aColWidths[$iColCount] Local $aAlign[$iColCount] ; get max width For $j = 0 To $iColCount - 1 $aColWidths[$j] = 0 For $i = 0 To $iRowCount - 1 $aColWidths[$j] = $aColWidths[$j] > StringLen($aArray[$i][$j]) ? $aColWidths[$j] : StringLen($aArray[$i][$j]) Next Next ; Alignment initialize If $sAlign <> "" Then Local $aRawAlign = StringSplit($sAlign, ",", 2) Local $iRawCnt = UBound($aRawAlign) For $j = 0 To $iColCount - 1 If $j >= $iRawCnt Then $aAlign[$j] = "L" Else $aAlign[$j] = StringStripWS(StringUpper($aRawAlign[$j]), 3) If Not StringRegExp($aAlign[$j], "^[LRC]$") Then $aAlign[$j] = "L" EndIf Next Else For $j = 0 To $iColCount - 1 $aAlign[$j] = "L" Next EndIf ; Create the header row For $i = 0 To $iColCount - 1 $sTable &= "| " & __FormatCell($aArray[0][$i], $aColWidths[$i], $aAlign[$i]) & " " Next $sTable &= "|" & @CRLF ; Create the alignment row $sTable &= "|" For $i = 0 To $iColCount - 1 Switch $aAlign[$i] Case "L" $sTable &= " :" & _StringRepeat("-", $aColWidths[$i] - 1) & " " Case "R" $sTable &= " " & _StringRepeat("-", $aColWidths[$i] - 1) & ": " Case "C" $sTable &= " :" & _StringRepeat("-", $aColWidths[$i] - 2) & ": " Case Else $sTable &= " " & _StringRepeat("-", $aColWidths[$i]) & " " EndSwitch $sTable &= "|" Next $sTable &= @CRLF ; Create the data rows For $i = 1 To $iRowCount - 1 For $j = 0 To $iColCount - 1 $sTable &= "| " & __FormatCell($aArray[$i][$j], $aColWidths[$j], $aAlign[$j]) & " " Next $sTable &= "|" & @CRLF Next ; remove the extra coloumn If Not $b2D Then $sTable = StringRegExpReplace($sTable, "(\| \||\| : \||\| :: \|)", "|") Return $sTable EndFunc ;==>_ArrayToMdTable ;--------------------------------------------------------------------------------------- Func __FormatCell($text, $width, $align) ; internal Switch $align Case "R" Return StringFormat("%" & $width & "s", $text) Case "C" Local $pad = $width - StringLen($text) Local $left = Floor($pad / 2) Local $right = $pad - $left Return _StringRepeat(" ", $left) & $text & _StringRepeat(" ", $right) Case Else ; "L" Return StringFormat("%-" & $width & "s", $text) EndSwitch EndFunc ;==>__FormatCell ;--------------------------------------------------------------------------------------- Please, every comment is appreciated! leave your comments and experiences here! Thank you very much Edited May 8 by ioa747 SOLVE-SMART 1 I know that I know nothing
SOLVE-SMART Posted May 8 Posted May 8 Well done @ioa747 👌 . Funny because I wrote in the other thread here about how I did rewrote your _StringToTable UDF. I am pretty sure I will review your new feature and will add it in a similar style to my DataToTable UDF. Thanks 😀 . Best regards Sven ioa747 1 ==> AutoIt related: 🔗 GitHub, 🔗 Discord Server, 🔗 Cheat Sheet Spoiler 🌍 Au3Forums 🎲 AutoIt (en) Cheat Sheet 📊 AutoIt limits/defaults 💎 Code Katas: [...] (comming soon) 🎭 Collection of GitHub users with AutoIt projects 🐞 False-Positives 🔮 Me on GitHub 💬 Opinion about new forum sub category 📑 UDF wiki list ✂ VSCode-AutoItSnippets 📑 WebDriver FAQs 👨🏫 WebDriver Tutorial (coming soon)
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now