Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

convert an array to hash

0 views
Skip to first unread message

Ramprasad A Padmanabhan

unread,
Nov 28, 2002, 3:17:09 AM11/28/02
to begi...@perl.org
Which is the quickest way of converting an array to hash keys

I have an array
@ARRAY = qw ( a b c d e );

# Convert array to hash keys , so can easily check for exists
# The values of the hash are immaterial to me

@HASH{@ARRAY}=@ARRAY;

....
....
foreach (@SOME_OTHER_ARRAY) {
next if(exists($HASH{$_})); # This is why I require a hash
.....
....
}


Can I have a faster way than this @HASH{@ARRAY} = @ARRAY;

Thanks
Ram

Paul Johnson

unread,
Nov 28, 2002, 3:47:28 AM11/28/02
to ramp...@ho.netcore.co.in, begi...@perl.org

Are you finding it slow?

@HASH{@ARRAY} = () _might_ be faster, but are you sure this is a bottleneck?

--
Paul Johnson - pa...@pjcj.net
http://www.pjcj.net

Sudarshan Raghavan

unread,
Nov 28, 2002, 3:51:01 AM11/28/02
to Perl beginners

Why do you think this is slow? Some other options are to use for or map...
I did a benchmark and a hash slice seems to be the fastest. If you don't
care about the values you can do this
@HASH{@ARRAY} = ();

Ramprasad A Padmanabhan

unread,
Nov 28, 2002, 4:21:56 AM11/28/02
to begi...@perl.org
No I did not mean to say I was finding this way as slow . But It has
happened so many times before that there have been better ways to do
things which I have been doing and many of them were so commonplace
that I am suprised they didnt occur to me .

So I just wanted to make sure I was not goofing again.
Thanks for the reply
Ram

0 new messages