* a. What is a size of an array in Perl?

In terms of bytes?
I don't know.

In terms of elements?
scalar(@array) (or simply @array in a scalar context).

--> What is the index of the first element of an array in Perl?

$[, which is usually assumed to be 0. (Updated)

--> What is the index of the last element of an array in Perl?

$#array (which is the same as @array - 1).

* b. How to find whether a certain element is present in an array?

I presume you're asking for the best way to do so, but the choice of which is best depends on the definition of element, on whether the array is sorted, on the size of the array, on whether a single lookup is done or not, and on how much effort you want to spend.

* c. What is the index of an element in an array?...

You must locate the element yourself. See (b).

* 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?

Programming is more than just knowing the language. That's why I spent 4 years in university instead of just reading the docs.

Or at least could anybody please post [a binary search] (for starters)

There's a module on CPAN (Search::Binary) and there's one on my scratchpad, copied here for your convenience:

sub _unsigned_to_signed { return unpack('i', pack('I', $_[0])); } sub binsearch(&$\@) { my ($compare, $value, $array) = @_; # # If the $value is not found in @array, # the 1s complement of where the $value # should be inserted is returned. So, # $value is found if the return value >= 0. # # When compare is called, $a is an alias for $value, # and $b is an alias for the element of the array # against which $a which be compared. # # Example: # # Add $value to sorted @array, if it's not already there. # $idx = binsearch { $a <=> $b } $value, @array; # splice(@array, ~$idx, 0, $value) if ($idx < 0); # my $i = 0; my $j = $#$array; return $j if ($j == -1); my $ap = do { no strict 'refs'; \*{caller().'::a'} }; my $bp = do { no strict 'refs'; \*{caller().'::b'} }; local *$ap; local *$bp; *$ap = \$value; for (;;) { my $k = int(($i+$j)/2); *$bp = \($array->[$k]); my $cmp = &$compare(); return $k if ($cmp == 0); if ($cmp < 0) { $j = $k-1; return _unsigned_to_signed(~$k) if ($i > $j); } else { $i = $k+1; return _unsigned_to_signed(~$i) if ($i > $j); } } }

It's not quite to your spec without this wrapper:

sub my_binsearch($\@) { unshift(@_, sub { $a <=> $b }); my $idx = &binsearch; undef $idx if $idx < 0; return $idx; }

Update: binsearch now uses the caller's package's $a and $b.

Update: Fixed bugs in binsearch.

Test proggy:

my @unsorted = qw( ... strings ... ); my @sorted; foreach (@unsorted) { my $word = lc($_); my $idx = binsearch { $a cmp $b } $word, @sorted; splice(@sorted, ~$idx, 0, $word) if ($idx < 0); } print(join("\n", @sorted), "\n");

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

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.