PowerShell v3+, 55 bytes
param($a,$b)1..$b|%{"|$((' ','-')[$_-in1,$b]*($a-2))|"}
Takes input $a
and $b
. Loops from 1
to $b
. Each iteration, we construct a single string. The middle is selected from an array of two single-length strings, then string-multiplied by $a-2
, while it's surrounded by pipes. The resulting strings are left on the pipeline, and output via implicit Write-Output
happens on program completion, with default newline separator.
Alternatively, also at 55 bytes
param($a,$b)1..$b|%{"|$((''+' -'[$_-in1,$b])*($a-2))|"}
This one came about because I was trying to golf the array selection in the middle by using a string instead. However, since [char]
times [int]
isn't defined, we lose out on the savings by needing to cast as a string with parens and ''+
.
Both versions require v3 or newer for the -in
operator.
Examples
PS C:\Tools\Scripts\golfing> .\draw-an-ascii-rectangle.ps1 10 3
|--------|
| |
|--------|
PS C:\Tools\Scripts\golfing> .\draw-an-ascii-rectangle.ps1 7 6
|-----|
| |
| |
| |
| |
|-----|
Ị
:)