#200166 - 2010-10-06 12:13 PM
What is the easiest way to print an array?
|
Pax
Getting the hang of it
Registered: 2006-12-01
Posts: 51
|
This is part of my input validation from my script I'm running, but I thought a different thread would be best as so people don't trawl through the other mess to find a simple (hopefully) response.
I have an array and I want to print it to screen (but the location shouldn't matter), what is the easiest way?
I have seen UDFs to convert strings to arrays, but not vice versa.
The only other way I can thing of is to write a loop which iterates through the array and prints the items to the screen (which presumably a UDF would do anyway).
I don't mind doing the writing, but if the wheel is invented, then why carve another which will be rougher than the first.
Pax
|
|
Top
|
|
|
|
#200169 - 2010-10-06 01:46 PM
Re: What is the easiest way to print an array?
[Re: Mart]
|
Pax
Getting the hang of it
Registered: 2006-12-01
Posts: 51
|
An array has multiple element so you can display them one by one. Lets say your array holds the numbers 1 to 10. Displaying them on the screen could look like this.
For $i = 0 to UBound($array)
? $array[$i]
Next
Hey Mart,
About right, but as I want to diaply them to a user I'll put a comma in there and make the change as below. 'Please choose a valid brand from the list - ALL,'
For $i = 0 to UBound($validbrands)
$validbrands[$i] + ','
Next
@CRLF
I had done this part of the code, but wondered if there was a generic method for it.
Do I need the + in there these days? I see a bit of code written with it in, and yet others leave it out.
Pax
|
|
Top
|
|
|
|
#200171 - 2010-10-06 03:14 PM
Re: What is the easiest way to print an array?
[Re: Pax]
|
Mart
KiX Supporter
   
Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
|
.... Do I need the + in there these days? I see a bit of code written with it in, and yet others leave it out.
Pax
Work just fine with and without the +.
$array = "McDonalds", "Burger King", "KFC", "Dunkin Donuts", "IHOP"
For $i = 0 to UBound($array)
$array[$i] ","
Next
Sleep 5
Displays the same as:
$array = "McDonalds", "Burger King", "KFC", "Dunkin Donuts", "IHOP"
For $i = 0 to UBound($array)
$array[$i] + ","
Next
Sleep 5
Edited by Mart (2010-10-06 03:16 PM)
_________________________
Mart
- Chuck Norris once sold ebay to ebay on ebay.
|
|
Top
|
|
|
|
#200172 - 2010-10-06 03:54 PM
Re: What is the easiest way to print an array?
[Re: Mart]
|
Glenn Barnas
KiX Supporter
   
Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
|
Actually, the "easiest" way is to use the Join command:This might be less obvious than a For/Next loop, but can be done in one line. If you want multiple lines of output (one element per line) just change the "','" in the example to "@CRLF". That string defines the delimiter between the array elements. This also eliminates the trailing "," that results when using For Next without additional logic.
Join takes an array and combines it into a string, using the specified delimiter string. Key word is "string", since most of us think of a "delimiter" as a single character. Join can use one or more characters as its delimiter, so it is often used with Split to replace words. For example:$Text = 'The quick brown fox'
Join(Split($Text, 'brown'), 'Arctic') ? splits the string into an array containing "The quick " and " fox", discarding "brown", then Join puts the array back together with "Arctic" as the delimiter. The result is "The quick Arctic fox".
Glenn
_________________________
Actually I am a Rocket Scientist!
|
|
Top
|
|
|
|
#200176 - 2010-10-06 05:11 PM
Re: What is the easiest way to print an array?
[Re: Glenn Barnas]
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
I've got a ready-rolled UDF that I drag out of the toolchest every now and again for dumping complex variables - it's especially useful for nested arrays:
$asArray1="a","b","c"
$aiArray2=1,2,3,4
$avCompoundArray=$asArray1,$aiArray2
DumpVar($avCompoundArray,"avCompoundArray"," ")
Function DumpVar($vData,$sProperty,$sSpacer,Optional $sLevel)
Dim $iIndex,$vSub
$sVarType=VarTypeName($vData)
Left($sProperty+$sSpacer,Len($sSpacer))+Left($sVarType+$sSpacer,11)
Select
Case $sVarType="Boolean"
CStr($vData)+IIf(CInt($vData)," (TRUE)"," (FALSE)")+@CRLF
Case $sVarType="Date"
CStr($vData)+@CRLF
Case $sVarType="String"
CStr($vData)+@CRLF
Case $sVarType="long"
CStr($vData)+@CRLF
Case InStr($sVarType,"[]")
@CRLF
$iIndex=0
For Each $vSub in $vData
$iIndex=$iIndex+1
DumpVar($vSub," -> "+$sLevel+"["+$iIndex+"]",$sSpacer,$sLevel+"["+$iIndex+"]")
Next
Case "Default"
@CRLF
EndSelect
EndFunction
Example output:
avCompoundArray Variant[] -> [1] Variant[] -> [1][1] String a -> [1][2] String b -> [1][3] String c -> [2] Variant[] -> [2][1] Long 1 -> [2][2] Long 2 -> [2][3] Long 3 -> [2][4] Long 4 |
|
|
Top
|
|
|
|
#200177 - 2010-10-06 05:49 PM
Re: What is the easiest way to print an array?
[Re: Pax]
|
Pax
Getting the hang of it
Registered: 2006-12-01
Posts: 51
|
Wow, I never knew something like a little + could provoke strong opinions amongst people.
I think I'll continue to use +, because as everyone agrees, it is good practice.
As to the ?, I'll take it under advisement. I generally use @CRLF when using WRITEVALUE to perhaps I should set my ways down one path, but as it requires training of my mind it will take a little time.
I'll look at the other options printing the other elements to screen as I do like compact code and 1 line JOIN does look neat.
Pax
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 2924 anonymous users online.
|
|
|