http://www.perlmonks.org?node_id=103504


in reply to Fun with two-dimensional arrays

I don't know why you say the number of rows is hard coded. Why can't you just do this:

foreach my $row (0 .. $lastRow) { }

But you might consider not storing it as a 2-dimensional array, but rather as a 22x80 character-long string. Then traversal becomes simply a matter of using substr and multiplying indexes (and it duplicates the behavior of wrapping that most terminals exhibit):

my $screen = ' ' x ($rows * $columns); foreach my $row (0 .. $rows-1) { my $rowText = substr($screen, $row*$columns, $columns); } # sets text, returns old text. sub setTextAt { my $x = shift; my $y = shift; my $text = join('', @_); substr($screen, $y * $columns + $x, length($text), $text); }