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
| [reply] [d/l] |
| [reply] [d/l] [select] |
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
| [reply] [d/l] |
Arrays have many uses and sometimes perl programmers can overuse hashes, but as an ex-Tcl programmer myself I can give the advice to:
- use hashes when you can
- 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!
- 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?
| [reply] |