Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: “A meeting at the Liquor-Vodka Factory”, or… same ARRAY questions again?!!

by ikegami (Patriarch)
on Sep 02, 2005 at 20:50 UTC ( #488786=note: print w/replies, xml ) Need Help??


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

* 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");

Replies are listed 'Best First'.
Re^2: “A meeting at the Liquor-Vodka Factory”, or… same ARRAY questions again?!!
by Anonymous Monk on Sep 03, 2005 at 09:46 UTC

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

    0

    Actually,
    $[ The index of the first element in an array, and of the fir +st character in a substring. Default is 0, but you could theoretically set it to 1 to make Perl behave more like aw +k (or Fortran) when subscripting and when evaluating the index() + and substr() functions. (Mnemonic: [ begins subscripts.) As of release 5 of Perl, assignment to $[ is treated as a compiler directive, and cannot influence the behavior of a +ny other file. (That's why you can only assign compile-time constants to it.) Its use is highly discouraged. Note that, unlike other compile-time directives (such as strict), assignment to $[ can be seen from outer lexical s +copes in the same file. However, you can use local() on it to st +rictly bound its value to a lexical block.
      Shoot, you're right. I consider $[ deprecated, so I never think of it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://488786]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2023-02-09 02:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (44 votes). Check out past polls.

    Notices?