I am writing a script which would tile X11 windows dynamically in Perl. So far I am going to use an array of arrays containing windows coordinates, sizes and ids to store state of tiled windows during session. Is it a good idea or should I organize this information in any other way?
The appropriate structure depends on how you will be accessing and processing the data structures. Choosing the correct structure is a large part of solving a program. Choose the wrong structure and a simple problem can grow difficult. Structure is so important that sometimes you need to transform an existing structure into a form that is more amenable to the sort of work you need to carry out.
Here are two basic rules that will help you select structural elements:
So if you just want to count the windows, find rectangular number with an aspect ratio similar to the screen that is greater than the number of windows, and then tile the windows one at a time: just use an array.
If you want to do a bunch of things like look up windows by application name or other things that require many different lookups, use a hash.
If you need to do many lookups over several keys, as well as maintain an order, you can make multiple data structures that point to the same underlying references.
my @foo = (
{ name => 'a', id => '321' },
{ name => 'b', id => '123' },
);
my %foo_by_name = map { $_{name} => $_ } @foo;
my %foo_by_id = map { $_{id } => $_ } @foo;
If you need to manipulate your complex collection (add and remove elements), consider wrapping the various structures in an object that will ensure that all the underlying structures are consistently managed.