Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Need HELP with array and hash table comparison

Reply
Thread Tools

Need HELP with array and hash table comparison

 
 
Saya
Guest
Posts: n/a
 
      04-19-2004
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

/
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      04-19-2004
On Mon, 19 Apr 2004, Saya wrote:

> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Benchmark segfault [Was: Array#inject to create a hash versus Hash[*array.collect{}.flatten] ] Michal Suchanek Ruby 6 06-13-2007 04:40 AM
Array#inject to create a hash versus Hash[*array.collect{}.flatten] -- Speed, segfault Anthony Martinez Ruby 4 06-11-2007 08:16 AM
Array#uniq - Comparison doesn't use 'eql?' and 'hash' Wolfgang Nádasi-Donner Ruby 0 02-20-2007 07:15 AM



Advertisments