audioman Posted May 5, 2020 Share Posted May 5, 2020 I'm trying to write a conditional statement which produces a string whose format is based on the 1, 2 or 3 names in an array. The number of names varies from 1 array to the next and a given cell either contains a name or is empty. If there are 3 names, the string format would be "name1, name2 and name 3." If there are 2 names the string would be "name1 and name2." Finally, 1 name would simply be "name1." I've tried a number of ways to do this including $array[1] <>"", $array = "" and $array[1] = Null. Attached is one of my many attempts to write the statement. Any help would be apprecieted. conditionalStatement.txt Link to comment Share on other sites More sharing options...
water Posted May 5, 2020 Share Posted May 5, 2020 Somethin like this? Global $aNames[][] = [["Name-1-1", "Name-1-2", "Name-1-3"], ["Name-2-1", "Name-2-2"], ["", "Name-3-1"], ["", "Name-4-1", "Name-4-2"], ["Name-5-1", "", "Name-5-2"]] For $i = 0 To UBound($aNames, 1) - 1 ; Count number of names in each row $iCount = 0 For $j = 0 To UBound($aNames, 2) - 1 If $aNames[$i][$j] <> "" Then $iCount = $iCount + 1 Next ; Create the output string $sOut = "" For $j = 0 To UBound($aNames, 2) - 1 If $aNames[$i][$j] <> "" Then Switch $iCount Case 1 ; one name in the row $sOut = $aNames[$i][$j] ExitLoop Case 2 ; two names in the row If $sOut = "" Then ; name one of two $sOut = $aNames[$i][$j] Else ; name two of two $sOut = $sOut & " and " & $aNames[$i][$j] ExitLoop EndIf Case 3 ; three names in the row If $sOut = "" Then ; name one of three $sOut = $aNames[$i][$j] & ", " ElseIf StringRight($sOut, 2) = ", " Then ; name two of three $sOut = $sOut & $aNames[$i][$j] & " and " Else ; name three of three $sOut = $sOut & $aNames[$i][$j] EndIf EndSwitch EndIf Next ConsoleWrite($sOut & @CRLF) Next careca 1 My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
audioman Posted May 6, 2020 Author Share Posted May 6, 2020 Hi Water: Thanks for the script. My description was a little misleading so, if I'm understanding it correctly, it's more elaborate than what I need. This code will be part of a script which will be executed once so the 1, 2, 3 name format will be required just once as well. I'm hoping I can modify it to fit that need. Link to comment Share on other sites More sharing options...
water Posted May 6, 2020 Share Posted May 6, 2020 Simply add your $aArray definition to my code and replace all occurrences of $aNames with $aArray. Done. My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
mikell Posted May 7, 2020 Share Posted May 7, 2020 (edited) Many ways to skin this cat ($is_mikell ? thingy : cat) #Include <Array.au3> Global $aNames[][] = [["Name-1-1", "Name-1-2", "Name-1-3"], ["Name-2-1", "Name-2-2"], ["", "", "Name-3-1"], ["", "Name-4-1", "Name-4-2"], ["Name-5-1", "", "Name-5-2"]] ;_ArrayDisplay($aNames) ; option 1 $s = StringRegExpReplace(StringRegExpReplace(StringRegExpReplace(_ArrayToString($aNames), _ '(?m)^\|+|\|+$', ""), '(?m)\|+([^\|]+)$', " and $1"), '\|+', ", ") Msgbox(0,"", $s) ; option 2 Local $res[UBound($aNames)] For $i = 0 to UBound($aNames)-1 $s = $aNames[$i][0] & "|" & $aNames[$i][1] & "|" & $aNames[$i][2] $res[$i] = _Format($s) Next _ArrayDisplay($res) Func _Format($string) Return StringRegExpReplace(StringRegExpReplace(StringRegExpReplace($string, _ '^\|+|\|+$', ""), '\|+([^\|]+)$', " and $1"), '\|+', ", ") EndFunc Edited May 8, 2020 by mikell Link to comment Share on other sites More sharing options...
water Posted May 7, 2020 Share Posted May 7, 2020 I fear there are more ways to skin a cat than cats My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
mikell Posted May 8, 2020 Share Posted May 8, 2020 Edited Link to comment Share on other sites More sharing options...
water Posted May 8, 2020 Share Posted May 8, 2020 😂 My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
audioman Posted May 13, 2020 Author Share Posted May 13, 2020 Hi Water: I really like your script. However, I'm trying to get my data which is in another array into the script. The 3 names are in $g_aArray[0][6], $g_aArray[0][7] and $g_aArray[0][8]. I've tried a number of approaches but I always end up with a list that looks like this: Name-1-1, Name-1-2 and Name-1-3 Name-2-1 and Name-2-2 Name-3-1 Name-4-1 and Name-4-2 Name-5-1 and Name-5-2 Can you point me in the right direction? Link to comment Share on other sites More sharing options...
water Posted May 13, 2020 Share Posted May 13, 2020 (edited) You could modify mikell's example (untested): #Include <Array.au3> Global $g_aArray[][] = [["", "", "", "", "", "", "Name-1-1", "Name-1-2", "Name-1-3"], ["", "", "", "", "", "", "Name-2-1", "Name-2-2"], ["", "", "", "", "", "", "", "", "Name-3-1"], ["", "", "", "", "", "", "", "Name-4-1", "Name-4-2"], ["", "", "", "", "", "", "Name-5-1", "", "Name-5-2"]] Local $res[UBound($aNames)] For $i = 0 to UBound($g_aArray)-1 $s = $g_aArray[$i][6] & "|" & $g_aArray[$i][7] & "|" & $g_aArray[$i][8] $res[$i] = _Format($s) Next _ArrayDisplay($res) Func _Format($string) Return StringRegExpReplace(StringRegExpReplace(StringRegExpReplace($string, _ '^\|+|\|+$', ""), '\|+([^\|]+)$', " and $1"), '\|+', ", ") EndFunc Edited May 13, 2020 by water My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
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