Sylvia
Sylvia

Reputation: 2616

Powershell - concatenating an array reference with another variable

I'm trying to concatenate a variable with a value from an array, and having problems.

The output I get is the variable value, plus the ENTIRE array, not just the desired value (index of $i).

$Database = "Checklist.dbo."

$ExtractTables = @("Page"
    , "HotelOwner"
    , "Hotel"
)

for ($i=0; $i -le $ExtractTables.Length – 1; $i++)  {

    write-host $Database$ExtractTables[$i]     # <<<<<<< takes ENTIRE array
}

My ultimate goal is to call an executable (bcp) something like this:

# & bcp $Database$ExtractTables[$i] out $OutputDirectory$ExtractTables[$i].txt -c -T -SCHELDEV02

Any pointers for a newbie?

Thanks!

Upvotes: 0

Views: 1337

Answers (2)

Bill Barry
Bill Barry

Reputation: 3543

$Database = "Checklist.dbo."

$ExtractTables = @("Page"
    , "HotelOwner"
    , "Hotel"
) 

$ExtractTables | ForEach {
    write-host "$Database$_"
}

Upvotes: 0

jon Z
jon Z

Reputation: 16646

$Database = "Checklist.dbo."

$ExtractTables = @("Page"
    , "HotelOwner"
    , "Hotel"
)

for ($i=0; $i -le $ExtractTables.Length – 1; $i++)  {
    write-host "$Database$($ExtractTables[$i])"
}

To evaluate a sub-expression before the rest of the expression put it inside $()

Upvotes: 1

Related Questions