Can you throw some light on this statement: my %hashset = map { $_ => 1 } @array;
On Thu, Mar 20, 2008 at 9:14 PM, Chas. Owens <[EMAIL PROTECTED]> wrote: > On Thu, Mar 20, 2008 at 11:35 AM, Sharan Basappa > > <[EMAIL PROTECTED]> wrote: > > > Well here is what I am trying to do. > > I have an array (generated from somewhere) I would like to convert > > this into an associative array and then based on some other input > > I would like to see if an entry exists in the asso array. As you can see > > the value does not really matter. What matters is whether the entry > > exists or not. > > For example (very trivial one) > > I have a list of names that are allowed access to a machine. I create a > asso > > array of them. Later when I want to check if the user is allowed to > login, I > > check if that user exists or not. Currently I am doing this by making > value for > > key as 1. But I never really use the value at all. > > Regards > snip > > This data structure is called a hash set. Hash sets are easy to make in > Perl: > > #!/usr/bin/perl > > use warnings; > use strict; > > my @array = qw<a d f h>; > my %hashset = map { $_ => 1 } @array; > for my $letter ("a" .. "i") { > if (exists $hashset{$letter}) { > print "$letter is in the hash set\n"; > } else { > print "$letter is not in the hash set\n"; > } > } > > a is in the hash set > b is not in the hash set > c is not in the hash set > d is in the hash set > e is not in the hash set > f is in the hash set > g is not in the hash set > h is in the hash set > i is not in the hash set > > > -- > > > Chas. Owens > wonkden.net > The most important skill a programmer can have is the ability to read. > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/