Page 1 of 1 1
Topic Options
#200166 - 2010-10-06 12:13 PM What is the easiest way to print an array?
Pax Offline
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
#200167 - 2010-10-06 12:23 PM Re: What is the easiest way to print an array? [Re: Pax]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
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.

 Code:
For $i = 0 to UBound($array)
	? $array[$i]
Next
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#200169 - 2010-10-06 01:46 PM Re: What is the easiest way to print an array? [Re: Mart]
Pax Offline
Getting the hang of it

Registered: 2006-12-01
Posts: 51
 Originally Posted By: Mart
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.

 Code:
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.
 Code:
  '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 Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
 Originally Posted By: Pax

....
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 +.

 Code:
$array = "McDonalds", "Burger King", "KFC", "Dunkin Donuts", "IHOP"

For $i = 0 to UBound($array)
	$array[$i] ","
Next
Sleep 5


Displays the same as:

 Code:
$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 Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Actually, the "easiest" way is to use the Join command:
 Code:
Join($array, ',') ?
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:
 Code:
$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! \:D

Top
#200173 - 2010-10-06 04:01 PM Re: What is the easiest way to print an array? [Re: Glenn Barnas]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
The "+" is a string contatenation operator. The difference is important to understand..
 Code:
"This " + "That" @CRLF
combines the two strings and then outputs the result (since there is no LValue in this operation, Kix simply displays the result on the console).
 Code:
"This " "That" @CRLF
has the same output result, but consists of two distinct output operations.

For most situations, there's no difference, but if you use functions for output management or formatting, it's important to use the string concatenation format (S + S...) so the entire string is "built" and then passed as a single argument to the function.

If you use output functions often, or pass compound strings to functions, it's a good habit to use the "+", simply because it can translate directly from a simple console output to a managed log write function. (see the Msg() and fMsg() UDFs on my site for an example of manage output functions).

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#200174 - 2010-10-06 04:04 PM Re: What is the easiest way to print an array? [Re: Pax]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
 Originally Posted By: Pax
Do I need the + in there these days? I see a bit of code written with it in, and yet others leave it out.


There is no implicit catenation of the elements, each remains a seperate token.

Not using "+" has a couple of drawbacks:
1. If you don't use "+" then each element is sent to the output as a separate system call. Not too much of a problem for a couple of debug lines, but generally not good programming technique.
2. If you decide to send your output somewhere else such as a file or logging function then the spaces between the elements will become a syntax error.
3. Your intent is less explicit, so the code is not so easy to read or debug.

Good practice is therefore to catenate the tokens together using "+" to create the final string and only then send it on its way.

No-one is going to be offended if you don't use "+", but expect sniggers if you ask why Log("Value is " $Value) doesn't work.

Similarly, avoid using "?" as a print statement. It isn't and should be avoided altogether. Pretend that you never knew it existed and you will be doing yourself a favour.

Top
#200175 - 2010-10-06 04:06 PM Re: What is the easiest way to print an array? [Re: Richard H.]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
 Originally Posted By: Richard H
Similarly, avoid using "?" as a print statement. It isn't and should be avoided altogether. Pretend that you never knew it existed and you will be doing yourself a favour.
AMEN!

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#200176 - 2010-10-06 05:11 PM Re: What is the easiest way to print an array? [Re: Glenn Barnas]
Richard H. Administrator Offline
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:
 Code:
$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 Offline
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
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 2924 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.07 seconds in which 0.033 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org