Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: lsearch for perl?

by pileofrogs (Priest)
on Dec 05, 2005 at 16:59 UTC ( [id://514173]=note: print w/replies, xml ) Need Help??


in reply to lsearch for perl?

While this isn't really an answer to the "lsearch" question, it does address the "perl mentality" part of the question

When I first started using perl (after some time in C) I thought hashes were black magic and I did a lot of stuff with lists that I would now do with a hash. In fact, I use lists pretty rarely any more.

When you're using lists, ask yourself if it could be done more easily with a hash.

-Pileofrogs

Updated: Yes! Thank you thor and ikegami. I think that was what I was trying to say.

Replies are listed 'Best First'.
Re^2: lsearch for perl?
by thor (Priest) on Dec 05, 2005 at 17:37 UTC
    When you're using lists, ask yourself if it could be done more easily with a hash.
    In my opinion, they address different problems. Arrays are an ordered set of things, hashes are an unordered set of associations. Sure, if you find yourself doing something like this:
    my (@item, @color); while(<DATA>) { chomp; my($item, $color) = split(/,/); push @item, $item; push @color, $color; } for(my $i=0; $i < @item; $i++) { print "$item[$i] has color $color[$i]\n"; } __DATA__ apple,red orange,orange banana,yellow
    then yes...use a hash. Don't try to shoehorn it, though.

    thor

    The only easy day was yesterday

      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;
        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

      Arrays have many uses and sometimes perl programmers can overuse hashes, but as an ex-Tcl programmer myself I can give the advice to:
      1. use hashes when you can
      2. when an array is appropriate, use foreach, map and grep. Some perl programmers might overuse map and grep, but when they are good they are VERY good!
      3. if you find yourself using an array index ask yourself why - there is almost always a better way in perl
      For the enlightenment of non-Tcl people, Tcl has only two types of data. Strings and lists. That's it. Even function calls are just a list where the first list value is the name (string) of a function (or proc) and the rest of the list values are the arguments (which may be, you guessed it, strings or lists).

      The lists aren't even very convenient, vis. lsearch.

      Don't think I'm bashing Tcl - it's very useful when you need a straightforward embeddable scripting language. You can even make it do very non-trivial things (vis. OpenACS) but it hurts.

      upvar anyone?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-26 08:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found