Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^3: lsearch for perl?

by ikegami (Patriarch)
on Dec 05, 2005 at 18:18 UTC ( [id://514194]=note: print w/replies, xml ) Need Help??


in reply to Re^2: lsearch for perl?
in thread lsearch for perl?

While I agree that hashes wouldn't be too useful here (being unordered), I think of hashes as having three basic uses (not one):

  • Unordered set of associations (Notice the plural var name)
    my %user_passwds = ( joe => ..., jeff => ..., john => ..., );
  • Unordered set of properties (Notice the singular var name)
    my %user = ( id => 'joe', name => 'Joe', passwd => ..., home_dir => ..., );
  • Uniqueness constraint
    my %unique; ++$unique{$_} foreach @list; @list = keys %unique;

Replies are listed 'Best First'.
Re^4: lsearch for perl?
by thor (Priest) on Dec 05, 2005 at 19:40 UTC
    I meant association in a looser sense than you mention above. For the first set, you're associating passwords with their respective users. In the second set, you're associating the given values with their type ("joe" is the "id" for this user). As for the uniqueness condition, I agree that it's useful, but not a hash's intended use. Incidentally, I think that the following loop uses less memory:
    my %unique; $unique{$_} = undef foreach @list; @list = keys %unique;
    due to the undef being shared amongst all of the keys of the hash. That may be an urban legend that I heard somewhere, though. ;)

    thor

    The only easy day was yesterday

      I'm pretty sure undef uses less mem, and it's probably a bit faster, but ++ allows you to check which items had duplicates. undef @unique{@list}; is actually fastest.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2024-04-18 03:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found