开发者

Tiling windows with perl script - are nested arrays a bad idea?

开发者 https://www.devze.com 2023-02-17 23:05 出处:网络
I am writing a script which would tile X11 windows dyn开发者_运维知识库amically in Perl. So far I am going to use an array of arrays containing windows coordinates, sizes and ids to store state of til

I am writing a script which would tile X11 windows dyn开发者_运维知识库amically 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:

  1. If you want to keep an order, use an array.
  2. If you need to do a lot of lookups for specific names, ids or other information, use a hash.

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.


Arrays of arrays, or arrays or hashes, or hashes of hashes, or hashes of arrays of hashes of hashes of arrays are wonderful in perl and one of the reasons that data processing in perl is so easy. You do not need to fear them!


Well, the standard representation of compound structures in Perl is a (blessed) hash. Unless you have serious performance concerns, grouping data into hashes would be a good idea.

OTOH, arrays are good for uniform data. But when you have data with unique ids (X window windows DO have ids, right?) a hash is a more natural structure, again. So you just say

do_something($windows->{$id}->{x});

and not

foreach (@$windows) { 
    if ($_->{id} == $id ) { 
        do_something($_->{x});
    }; 
}


The guideline I follow is: If maintaining the order of the elements is important, use an array; otherwise, use a hash.

Hashes are slower than arrays but by choosing meaningful keys they can be easier to maintain. So I prefer them over arrays.

0

精彩评论

暂无评论...
验证码 换一张
取 消