Skip to main content
added 79 characters in body
Source Link
Tor
  • 221
  • 1
  • 6

If you're tackling a drawing/rendering problem that involves creating, manipulating, and then printing a 2D array, I've found it shorter character-wise to instead deal with a 1D array that you access with $arr[$y*$width+$x] rather than $arr[$y][$x].

It's very simple and short to set up the array with blanks or zeroes with something like

$arr=@(" ")*$width*$height

Half the reason for using 1D arrays is that initialization is much shorter.

To easily print the 1D array as a "2D" array to console, you can use a width-based regular expression to split your array into rows. This will join your array into a string, and then split the string into equally sized chunks and print them in order, effectively printing your 1D array as a 2D array:

-join$arr-split"(.{$width})"|?{$_}

If you're tackling a drawing/rendering problem that involves creating, manipulating, and then printing a 2D array, I've found it shorter character-wise to instead deal with a 1D array that you access with $arr[$y*$width+$x] rather than $arr[$y][$x].

It's very simple and short to set up the array with blanks or zeroes with something like

$arr=@(" ")*$width*$height

To easily print the 1D array as a "2D" array to console, you can use a width-based regular expression to split your array into rows. This will join your array into a string, and then split the string into equally sized chunks and print them in order, effectively printing your 1D array as a 2D array:

-join$arr-split"(.{$width})"|?{$_}

If you're tackling a drawing/rendering problem that involves creating, manipulating, and then printing a 2D array, I've found it shorter character-wise to instead deal with a 1D array that you access with $arr[$y*$width+$x] rather than $arr[$y][$x].

It's very simple and short to set up the array with blanks or zeroes with something like

$arr=@(" ")*$width*$height

Half the reason for using 1D arrays is that initialization is much shorter.

To easily print the 1D array as a "2D" array to console, you can use a width-based regular expression to split your array into rows. This will join your array into a string, and then split the string into equally sized chunks and print them in order, effectively printing your 1D array as a 2D array:

-join$arr-split"(.{$width})"|?{$_}
Source Link
Tor
  • 221
  • 1
  • 6

If you're tackling a drawing/rendering problem that involves creating, manipulating, and then printing a 2D array, I've found it shorter character-wise to instead deal with a 1D array that you access with $arr[$y*$width+$x] rather than $arr[$y][$x].

It's very simple and short to set up the array with blanks or zeroes with something like

$arr=@(" ")*$width*$height

To easily print the 1D array as a "2D" array to console, you can use a width-based regular expression to split your array into rows. This will join your array into a string, and then split the string into equally sized chunks and print them in order, effectively printing your 1D array as a 2D array:

-join$arr-split"(.{$width})"|?{$_}