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


in reply to Reminder to self: must use Memoize more often!

Hello tobyink,

I use 'Memoize' and it is an awesome product. But before I knew about it, I did some memorization using a hash. More than 10 years ago I wrote the following code for an editor that I converted from 'traditional C' to Perl:

sub ATS { # PyrEdit assumes the HOME is col 1, row 1 our %CRToutput; my $col = int(shift); my $row = int(shift); my $Saved = "$col|$row"; if (exists $CRToutput{$Saved}) { return($CRToutput{$Saved}); } my $loc; if ( $SYS{"ATS"} eq "xterm" ) ## all 'xterm' compatibles { $loc = "\e\[$row\;$col"."H"; } else { ## PyrEdit assumes that HOME is col 1, row 1 if ( $SYS{"ATSDEC"} eq "Y" ) { $row--; $col--; } $loc = qx/tput cup $row $col/; chomp($loc); } $CRToutput{$Saved} = $loc; return($loc); }

In *nix the 'tput' command is very expensive, and since I had to support lots of different terminal types, I cached the results using a hash. The results were as good as the 'traditional C' after just a few seconds of use.

Just an alternative.

Update: The original sub didn't have the 'my's. It was only after joining PM and learning of the value of 'use strict; use warnings' that I went back and put the 'my's into the code. I'm sure I've updated it in more than one place. It's currently on my mind since I received a package of xml files on Thursday without any "\n" and I added an '-xml' option the next day. After 2-3 hours of coding, I have a XML formatter that show the proper indention and structure for all(mine) XML files.

"Well done is better than well said." - Benjamin Franklin