Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare an array of strings (on multiple lines)

Why does $dlls.Count return a single element? I try to declare my array of strings as such:

$basePath = Split-Path $MyInvocation.MyCommand.Path

$dlls = @(
    $basePath + "\bin\debug\dll1.dll",
    $basePath + "\bin\debug\dll2.dll",
    $basePath + "\bin\debug\dll3.dll"
)
like image 222
Bruno Avatar asked Jun 10 '16 23:06

Bruno


People also ask

How do you declare an array of strings?

You can also initialize the String Array as follows:String[] strArray = new String[3]; strArray[0] = “one”; strArray[1] = “two”; strArray[2] = “three”; Here the String Array is declared first. Then in the next line, the individual elements are assigned values.

How do I declare a string array in PowerShell?

PowerShell array of strings is the collection of string objects that is multiple strings are residing into the same collection which can be declared using String[], @() or the ArrayList and can be used in various ways like functions, in the file content, as a variable and can perform the multiple operations on the ...

How do I return multiple lines of a string?

Use """ (double quote) or ''' (single quote) for multiline string or use \ for seperating them. NOTE: """ and ''' should be used with care otherwise it may print too many spaces in between two lines.

Can you store strings in arrays?

If we declare a fixed array of char using the [] notation, then we can store a string consisting of the same number of characters in that location. Note that one extra character space for the terminating null byte should be considered when copying the character string to the array location.


1 Answers

You should use something like:

$dlls = @(
    ($basePath + "\bin\debug\dll1.dll"),
    ($basePath + "\bin\debug\dll2.dll"),
    ($basePath + "\bin\debug\dll3.dll")
)

or

$dlls = @(
    $($basePath + "\bin\debug\dll1.dll"),
    $($basePath + "\bin\debug\dll2.dll"),
    $($basePath + "\bin\debug\dll3.dll")
)

As your answer shows, the semicolons also work because that marks the end of a statement...that would be evaluated, similar to using parenthesis.

Alternatively, use another pattern like:

$dlls = @()
$dlls += "...."

But then you might want to use ArrayList and gain performance benefits...

See PowerShell array initialization

like image 51
Kory Gill Avatar answered Oct 11 '22 22:10

Kory Gill