Need HELP with array and hash table comparison

S

Saya

Hi,

I need help/hints to accomplish the following.

Assume I have a varible $x looking something like this:

$x = "test test <a href="www.google.com">google</a> and some more text
here <a href="www.perl.com">perl</a>"

Now I use HTML::LinkExtor to find the a hrefs and call a replace
function:
$p = HTML::LinkExtor->new(\&replaceURL, "");
$p->parse($x);

The replaceURL function contains an array @safeLinkArr, consisting of
hrefs that need not to be done anything to. For instance lets say that
www.perl.com is a safe href so therefore it does not need anything
"done" to it.

How can I best run through the values in the hash against the values
in the safeLinkArr ? Is there a way I can say something like "in" and
make the comparison that way or how should the foreach or for loops
look like ???

Thx for any help provided

/Saya

/
 
P

Paul Lalli

Hi,

I need help/hints to accomplish the following.

Assume I have a varible $x looking something like this:

$x = "test test <a href="www.google.com">google</a> and some more text
here <a href="www.perl.com">perl</a>"

this has both Perl syntax errors (double quotes aren't escaped) and HTML
syntax errors (unless your website has local files named "www.google.com"
and "www.perl.com"). Please try to post actual code whenever possible.
Now I use HTML::LinkExtor to find the a hrefs and call a replace
function:
$p = HTML::LinkExtor->new(\&replaceURL, "");
$p->parse($x);

The replaceURL function contains an array @safeLinkArr, consisting of
hrefs that need not to be done anything to. For instance lets say that
www.perl.com is a safe href so therefore it does not need anything
"done" to it.

How can I best run through the values in the hash against the values
in the safeLinkArr ? Is there a way I can say something like "in" and
make the comparison that way or how should the foreach or for loops
look like ???


Your description is vague, but I *think* what you're asking for is how to
test each element of an array to see whether or not it exists in the hash?

Well, if the array values are keys to a hash, you just do this:
foreach (@array){
if (exists $hash{$_}) {
# %hash contains this element of @array;
}
}

If, however, the array values are values to the hash, then you're
basically asking for the intersection of two lists. This is covered in a
FAQ you can obtain from
perldoc -q intersection

my %count;
foreach (@array, values(%hash)){
$count{$_}++;
}

my @inboth = grep { $count{$_} > 1 } keys %count;
my @inone = grep { $count {$_} == 1 } keys %count;

Now @inboth contains all the items that are in both the array and the
hash, and @inone contains all the items that are in one but not the other.

(Note, of course, that this assumes both the array and the hash contain
only unique values).

Hope this helps,
Paul Lalli
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top