I am sorry if I have implied this is the solution you have given me as I was referring to the previous post. Although I do not have much idea of programming, I can see that your approach is clearly more succinct and idiomatic. Nevertheless, I had difficulties in extending your example as I had some errors when using the two arrays (keys and menus), and the previous code seemed to work.
Having the previous code being qualified as ‘horrid’ has given the extra motivation, and I think this is how the code should look like:
[code]%{
my @keypad = qw(KP_Home KP_Up KP_Prior KP_Left KP_Begin KP_Right KP_End KP_Down KP_Next);
my @menus = qw(Utils Gent Oficina Internet Sistema Oci Chrome Xarxa Global);
my $i;
KEYPAD: Move window to desk with the keypad (no modifier)
$i = 0; my $code1 = join (“\n”, map { "Key $_ A N GotoDesk 0 " . $i++} @keypad);
alt: Move a window to a different desk
$i = 0; my $code2 = join (“\n”, map { "Key $_ W M MoveToDesk 0 " . $i++} @keypad);
ctrl: Show menus related with the desk
$i = 0; my $code3 = join (“\n”, map { “Key $_ A C Menu $menus[$i++]”} @keypad);
Shift: Go to a different desk and show menu related with the desk
$i = 0; my $code4 = join (“\n”, map { “Key $_ A S F-GotoDeskMenu 0 $i $menus[$i++]”} @keypad);
WindowKey: Move a window to a different desk and follow it
$i = 0; my $code5 = join (“\n”, map { "Key $_ W 4 F-MoveAndGotoDesk 0 " . $i++} @keypad);
my $code = “$code1 \n$code2 \n$code3 \n$code4 \n$code5”;
$code;
}%[/code]
Cheers!
Edit: I have benchmarked both pieces of code (with the Benchmark) module to see if the horridness of the code was something more related with performance than aesthetics:
[code]Benchmark: timing 100000 iterations of awesome, horrid…
awesome: -1 wallclock secs ( 0.68 usr + 0.00 sys = 0.68 CPU) @ 147058.82/s (n=100000)
horrid: 9 wallclock secs ( 8.55 usr + 0.00 sys = 8.55 CPU) @ 11695.91/s (n=100000)
Rate horrid awesome
horrid 11891/s – -92%
awesome 140845/s 1085% --[/code]
The moral of the story is that horrid code takes much longer!
Edit: Made small corrections to the code.