Re: Learning about hashes
by almut (Canon) on Oct 15, 2009 at 16:32 UTC
|
In case that - after having perused the linked resource(s) - there's still some specific problems or questions remaining, I'd encourage you to just ask here. The Monks will be happy to help anyone who's made a reasonable effort him- or herself. That's part of what this site is for.
Just (try to) clearly describe what you want to happen... for example by
showing what you've tried — ideally, some self-contained snippet of code together with sample data, not forgetting to mention what the desired/expected result is, and what happened instead.
| [reply] |
|
|
Thanks for all the suggestions, but I'm still hitting a road block. I don't want to post too much of my code as I am trying to really understand hashes instead of someone just fixing my code.
I think a better question for me now is, how can I pass variables into a hash. Here is a snipit of my code, the variables are defined outside of the while loop.
while <(inFile)> {
my @record = split /\|/, $_;
my %eslist = $record[3];
}
Ultimately I want to output the unique entries in eslist but I can't even get it to count the number of keys in the hash and I'm getting a "odd number of elements in hash assignment" errors when using use warnings. I'm guessing there is something wrong with how I am trying to pass $record3 into the hash.
When defining the hash I've tried %eslist; and %eslist=(); but I still get the same errors. | [reply] [d/l] |
|
|
my %eslist = ($record[3] => 1);
True laziness is hard work
| [reply] [d/l] |
|
|
|
|
|
|
(W) You specified an odd number of elements to initialize a hash,
which is odd, because hashes come in key/value pairs.
What is in $record[3]? What other data do you want to associate with $record[3]?
If you show us a small sample of data, we could probably suggest how you might store the data in a hash.
Give perldata a quick read. | [reply] [d/l] [select] |
|
|
|
|
OK, I think I see what you are trying to do, but a hash may not be what you want. A list might be better for your needs.
List ex:
use List::MoreUtils qw(any); #instead of using:
# "if (grep( m/whatever/, @list)){}"
#use: "if (any { m/whatever/} @list){}"
#this exits on the first occurrence.
if (!any {m/^\Q$record[3]\E$} @esList){
#the \Q\E guarantees that the value held in the $record[3]
#is not interpreted as a regular expression
push (@esList = $record[3]);
}
### @esList is now a unique list of the values you want...
But if you really want to use a hash, this is how you might do it:
my %esList = ();
$esList{$record[3]) = 1;
### then to recall all the found records:
foreach my $result {sort(keys(%esList))){
print "found record: $result \n";
}
Also, if you're interested, I started a discussion recently about how to best implement multidimensional hashes, they have some unique characterictics that you might not expect when trying to extending principals of a one dimensional hash. But there are some very good references listed there. It can be found at: Best Multidimensional Hash Practices?
Good luck... | [reply] [d/l] [select] |
Re: Learning about hashes
by toolic (Bishop) on Oct 15, 2009 at 15:49 UTC
|
| [reply] |
Re: Learning about hashes
by BioLion (Curate) on Oct 15, 2009 at 16:20 UTC
|
Maybe also the perldocs as well? I always find them useful and i like the FAQ style example code : this and this. Maybe also the camel book / cookbook (both o'reilly press) if you have them?
Just a something something...
| [reply] |
Re: Learning about hashes
by planetscape (Chancellor) on Oct 16, 2009 at 00:24 UTC
|
| [reply] |
Re: Learning about hashes
by cmac (Monk) on Oct 16, 2009 at 07:42 UTC
|
Think of a %hash as two arrays, @keys and @values. The number of elements in @keys is always equal to the number of elements in @values.
Storing in a hash $hash{$key} = $value; is like:
for ($i=0; $i < @keys; $i++) {
if ($keys[$i] eq $key) {last} # 'last' exits from the for loop
}
if ($i < @keys) {
# $key already exists in the hash
$values[$i] = $value;
} else {
# $key is new
push @keys, $key;
push @values, $value;
}
| [reply] [d/l] [select] |