Owing to a, erm, bug(?), this won't work exists is error-prone and may autovivify the hash key whether it existed or not under some circumstances (see exists and search on the page for 'autovivication'). I agree with the idea of using a hash, but I use 'defined' instead of exists, i.e, something like:
my %hash = (); my $dummy = map( $hash{ $_ } = 1, @array ); my $checkthis = 'at'; ... if ( defined( $hash{ $checkthis )) { ... }
The best way to convert the array to hash is unclear without seeing what it was used for earlier in the code - map (above) is only one way to process the array and the assumption anyway is that you are reusing the list and can in the exact situation benefit from paying some fixed overhead in return for variable cost-reduction, otherwise the cost of making a hash is probably greater than a single pass through the list. This point is somewhat influenced by the surprisingly-overlooked fact that the single pass approach can on average execute only half the duration for a one-off pass by aborting when it succeeds, e.g.
my $found = ( $_ eq $checkthis ) && last for ( @array );
whereas in the first example, the array has to be passed right through plus the hash setup costs in order to make future lookups cheaper - I would estimate the reuse count to be about 4 - 6 to break even on the deal, with more uses profiting and less losing.

Update 1: rephrased - I did not mean to imply that the bug always happens.

Update 2 BUT, in actual work I always use the arrow operator (under which conditions the bug with exists would apply) and therefore never use exists - this kind of thing is unavoidably common in occurrence:

use strict; use warnings; Package Company; sub new { my $self = shift; my $regno = shift or die "Must supply reg. no."; $self = {}; $self -> { REGNO } = $regno; return bless $self; } sub parent { my $self = shift; my $parent; unless ( $parent = shift() ) { if (defined $self -> { PARENT } ) { return $self -> { PARENT }; else { return undef(); } } if ( defined $parent -> { REGNO } ) { $self -> { PARENT } = $parent; } else { die "Invalid parent reference"; }
the only reason I would need to use exists instead of defined is if I have a habit of defining keys with a value equal to undef(). Instead I have a habit of not defining the key.

-M

Free your mind


In reply to Re^2: searching a list by Moron
in thread searching a list by keiusui

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.