in reply to
Help needed to make a conditional statement
foreach $input (@in) { $data{$input}++ }
foreach $db (@db) { $data{$db}++ }
@db = [];
foreach $value (keys %data) {if ($data{$value}) = 1 {push @db $value} }
What does this do?
Assuming @in =
1,3,5
and @db =
3, 5, 6
First foreach %data = { 1=>1, 3=>1, 5=>1}
Second foreach %data = { 1=>1, 3=>2, 5=>2, 6=>1}
Now when the lists are combined, all keys with values
equal to 1 only occurs once, any keys that have values
greater than 1 occur in both lists.
Comment on
Re: Help needed to make a conditional statement
Replies are listed 'Best First'.
Re: Re: Help needed to make a conditional statement
by
Anonymous Monk
on Dec 05, 2002 at 08:30 UTC
One additional note, You can also add 1 to %data for the input and 2 to %data for the db.
Now your hash would look as followes:
%data = { 1=>1, 3=>3, 5=>3, 6=>2}
As you can see, now you can distinguish between keys from each array as
well as the union between the two.
needles
[reply]
Re: Re: Re: Help needed to make a conditional statement
by
jonnyfolk
(Vicar)
on Dec 05, 2002 at 15:56 UTC
Thanks for your input. I have actually gone with
fever
's grep solution but I will give this a try to see if I can get my head round it!!
[reply]
In Section
Seekers of Perl Wisdom