Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

* 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":



  • 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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-24 20:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found