Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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:

  • a. What is a size of an array in Perl?
  • b. How to find whether a certain element is present in an array?
  • c. What is the index of an element in an array?...

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:

  • d. How many people come here seeking for the answers to the abovementioned questions?
  • e. How many people come here for these more than once in their lifetime?
  • f. How many leave without the answer they were hoping to get, -- or, worse, with a wrong answer?

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.


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

In reply to “A meeting at the Liquor-Vodka Factory”, or… same ARRAY questions again?!! by Mabooka-Mabooka

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (1)
As of 2023-06-04 14:18 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (22 votes). Check out past polls.