http://qs1969.pair.com?node_id=488776

Mabooka-Mabooka has asked for the wisdom of the Perl Monks concerning the following question:

Dear Patient Monks,

There’s a novel “A meeting at the Liquor-Vodka Factory” (“собрание на ликеро-водочном заводе”) by Russian author M. Zhvanetsky. Workers at a liquor factory are having a meeting. It is structured like this:

The chairman pours himself from a bottle and says: “Today, we are going to listen to our Director of the Transportation Department”.

The Director of the Transportation Department comes forward, pours himself from a bottle and makes some speech.

The chairman pours himself again and says: “Well… Now, let us listen to our Director of the Transportation Department. Please tell us how are things in our transportation”.

The Director of the Transportation Department comes forward again, pours himself, and talks some more.

The chairman: “All right, then. Now, how about our Transportation Department? Where’ s our Director of Transportation? Anybody seen him?”...

And so it goes and goes, until the Chairman says: “Well, the things seem to go right. Except it’s a re-eeeeal pity that today we couldn’t make it to hear our Director of the Transportation Department.”.


... That’s approximately what I feel myself when searching for answers to questions like:

I hear: “Boo!!!”,,, “NOT Again!”, “RTFM!!”, “RTFAQ!!!”…. But please stay with me for another minute, -- I’ll try to prove that the problem still exists.

To make myself clearer, let me ask you three more questions:

I am ready to bet that the ##s are high. And here’s why. Let’s take a look at a) in FAQs here: "How do I find the size of an array?":

It becomes obvious that there are at least 7 answers:-). No one proposes the one and only one best solution, the confusion on whether to add a 1 to the index of the last element persists, and nobody discusses performance implications of casting to a scalar vs. using the last index. But that’s probably OK: an answer CAN be found, it only takes some time….

As for questions b) and c), it’s unfortunately not a case. I mean: neither solution works for me.
(These are two similar questions, so let’s look just at the first one - "How to find out if X is an element in an array?"):

... I actually love asking this question during an interview (regardless of the programming language), and see whether the person jumps into writing some sort of a loop or asks: how big is an array? Is it sorted? Is it dynamic?... You know, one could spend hours talking about this stuff.

What is proposed here in FAQs is:

* Two algorithms: either - traversing an array or - duplicating its content in a hash (“associative array” that is? +) for a fast lookup; * Using ‘grep’ or ‘eq’; * Assuming that arrays look like qw /I am an array, yeah…/.

Which means: Arrays are data structures that are always

SMALL; UNSORTED (== no assumptions about distribution of elements); CONTAIN ONLY SHORT STRINGS
, and there’re no performance or memory problems whatsoever while dealing with them.

Hmm... guess what: that’s not exactly the set of conditions that ~some~ real-world applications have to deal with.

I assert that:

* Traversing an array should never be used for searching; * Using a hash is a pretty smart solution, except: - It has memory implication (hence not always can be used); - It slows down considerably for huge arrays (or, at least you h +ave to build your own hash structure with some pretty hashing functio +n); - In any case, questions arise: is it smart to build it every tim +e one needs a lookup? What if the contents of an array change all the + time?.., etc. * There are other algorithms in CS.

Besides, there’re integers, floats, doubles, and data structures appearing as elements of arrays sometime…

And on top of all, -- nobody has proposed a subroutine, every recipe's style is not a "production code". (That’s why the code that looks elegant at the 1st glance:

$X = "x"; @array = qw / x y z 1 2 3 /; return exists {map { $_ => 1 } @array}->{$X};

is not really useful: one cannot paste it to a sub to make it work.

So…… Thanks for bearing. I hope I've made a poin. What's missing is: Well-thought-through and unambiguous answers to these three questions at the very top of the "Array" FAQ list.
Or at least could anybody please post (for starters):

- a sub - that takes two arguments : an array of ints and an int; - assumes that the array is sorted; - uses binary search :-); - returns an index of the element or undef.



THANKS FOR READING!

Janitored by Arunbear - replaced pre tags with code tags, to prevent distortion of site layout