how to test if an element belongs to an array or hash

B

biomahui

If I don't use iteration, how to test if an element belongs to an array
or hash?
Thanks a lot.
 
B

Brian McCauley

If I don't use iteration, how to test if an element belongs to an array
or hash?

Although there is a possible interpretation to the above question I very
much doubt that it is really the question you really inteded to ask.
Please define the terms "an element" and "belongs an array or hash".

You can find out if a _string_ exists as a _key_ in a hash using
exists($my_hash{$the_string})

There is no direct way to see if a string appears in an array or in the
values of a hash - you need to use a loop or build a(nother) hash (for
details see FAQ).

Given a scalar variable it is only possbile to find out if that scalar
is part of some known array or hash using a loop (a bit like the one in
the FAQ but with a couple of backslashes).

To answer the question you actually asked...

There is (to all normal intents and purposes) no way given a scalar
variable to know if that scalar is an value in some unknown array or hash.

my $foo = 1;
my @bar = (2);

for my $element ( $foo, @bar ) {
print "The value is $element and it is ";
print "not " unless some_impossible_function($element);
print "an array element\n";
}

If some_impossible_function() could be written this would say:

The value is 1 and it is not an array element
The value is 2 and it is an array element
 
B

biomahui

Thanks.

Another question,
Is there a way to find the keyvals from their values without using
iteration. For instance;

$set{"apple"}="fruit";$set{"pear"}="fruit";$set{"banana"}="fruit";$set{"beef"}="meal";

I need to find "apple","pear" etc from "fruit".
Thanks!
 
A

Anno Siegel

Thanks.

Another question,
Is there a way to find the keyvals from their values without using
iteration. For instance;

$set{"apple"}="fruit";$set{"pear"}="fruit";$set{"banana"}="fruit";$set{"beef"}="meal";

I need to find "apple","pear" etc from "fruit".

Not without iteration, but you can use grep to speed it up and hide it
somewhat:

my @fruitkeys = grep $set{ $_} eq 'fruit', keys %set;

Anno
 
B

biomahui

Thank you. I am programming with genomic information. Thus I always try
to speed it up.
All of your information has helped me a lot already.
Sorry for my little knowledge on perl.
Thanks a lot.
 
J

Jim Keenan

Thanks.

Another question,
Is there a way to find the keyvals from their values without using
iteration. For instance;

$set{"apple"}="fruit";$set{"pear"}="fruit";$set{"banana"}="fruit";$set{"beef"}="meal";

I need to find "apple","pear" etc from "fruit".

my @fruit;
for (keys %set) {push(@fruit, $_) if $set{$_} eq 'fruit'; }

Jim Keenan
 
B

Brian Wakem

Thanks.

Another question,
Is there a way to find the coevals from their values without using
iteration. For instance;

$set{"apple"}="fruit";$set{"pear"}="fruit";$set{"banana"}="fruit" $set{"beef"}="meal";

I need to find "apple","pear" etc from "fruit".
Thanks!


It sounds like you are not using an efficient data structure for your needs.

Can I suggest a hash of hashes:-

my %set = ( fruit => { apple => 1, pear => 1, banana => 1 },
meal => { beef => 1 },
);
print "$_\n" for keys %{$set{'fruit'}};
 
I

ioneabu

John said:
wrote:


for a normal array: -> iteration
for hash, exists

How about this way, without iteration? I know you can fool it into
thinking that 'apple banana' is an element though.

#!/usr/bin/perl
use strict;
use warnings;

my @array = ('apple','banana','peach','pear','mango');
my $string = join ' ', @array;
my $input = $ARGV[0];
print "$input is a value of the array\n" if $string =~ /$input/;
 
B

biomahui

Yes.

Hash in hash is really a good choice for me.

I heard of it today, and learned it today. It's so useful.

Thanks all of your suggestions.

hui
 
B

biomahui

A new question related:

if I created a hash, $set{$a}{$b}=1, I can get the keys of $b for a
given $a, using "keys %{$set{$a}}";

My question is, Is there any function that can test the existence of an
element belongs to $b for a given $a?
Thanks!
 
B

Brian Wakem

A new question related:

if I created a hash, $set{$a}{$b}=1, I can get the keys of $b for a
given $a, using "keys %{$set{$a}}";

My question is, Is there any function that can test the existence of an
element belongs to $b for a given $a?
Thanks!


I think you mean this:-

if (exists $set{'fruit'}{'pear'}) {
print "Pear is a fruit\n";
}
else {
print "Pear in not a fruit\n";
}

If not, please clarify.
 
J

John Bokma

wrote:
John said:
wrote:


for a normal array: -> iteration
for hash, exists

How about this way, without iteration? I know you can fool it into
thinking that 'apple banana' is an element though.

#!/usr/bin/perl
use strict;
use warnings;

my @array = ('apple','banana','peach','pear','mango');
my $string = join ' ', @array;
my $input = $ARGV[0];
print "$input is a value of the array\n" if $string =~ /$input/;

It still is iteration.
 
J

Jazeker

Thank you. I am programming with genomic information. Thus I always try
to speed it up.
All of your information has helped me a lot already.
Sorry for my little knowledge on perl.
Thanks a lot.

Just me wondering again. Why use Perl if mips are scarce compared to
data to process ?
 
B

biomahui

"Why use Perl if mips are scarce compared to data to process ? "

I don't understand this senetence. What's mips?
I don't know any other computer language, so I'm learning perl.
I do have a lot of data. The three files I am dealing with will create
three hash. The number of their keys are 2940862,568572,and 2346. I
need to correlate these data with two common IDs. (use ID1 to correlate
file A and B; use ID2 to correlate file A and C) One vaue for a hash
may have up to 12 keys.
It will take too much time to iterate, so I decide to save only the
values selected by hash. (It's said that hash runs faster than
iteration.)

Thanks for all of your concerns.
 
J

Jürgen Exner

<fullquote>
Yes.

I want to get this.
Thanks.
</fullquote>

Do you believe anybody can make any sense of this posting?
There is no way on earth to gess what you mean by "Yes" (maybe you are
responding to some question? What question?)
And what is "this"?

And thank you for thanking someone for something. Unfortunately nobody is
able to determine whom you are thanking or for what.

PLEASE, follow established guidelines that have been prooven for the last
20+ years and quote as much context as is needed to understand your posting.

jue
 
B

biomahui

I am sorry. Thanks Jurgen's suggestion. I used "reply" (the link below
someone's remark) to give my posting before, assuming I was replying
someone.
I realized now it was wrong.
So I tried "more options", then "reply", it works now.
I am sorry, but I am grateful to all people here. They contribute their
knowledge to me,directly or indirectly. I can always get quick response
here.
It's really a fantastic place where people are so friendly and helpful.
Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,253
Latest member
BlytheFant

Latest Threads

Top