Need HELP with array and hash table comparison

Discussion in 'Perl Misc' started by Saya, Apr 19, 2004.

  1. Saya

    Saya Guest

    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

    /
    Saya, Apr 19, 2004
    #1
    1. Advertising

  2. Saya

    Paul Lalli Guest

    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
    Paul Lalli, Apr 19, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    499
    red floyd
    Nov 10, 2011
  2. Wolfgang Nádasi-Donner

    Array#uniq - Comparison doesn't use 'eql?' and 'hash'

    Wolfgang Nádasi-Donner, Feb 20, 2007, in forum: Ruby
    Replies:
    0
    Views:
    87
    Wolfgang Nádasi-Donner
    Feb 20, 2007
  3. Wolfgang Nádasi-Donner

    Array#uniq - Comparison doesn't use 'eql?' and 'hash'

    Wolfgang Nádasi-Donner, Feb 20, 2007, in forum: Ruby
    Replies:
    3
    Views:
    118
    Daniel Finnie
    Feb 20, 2007
  4. Anthony Martinez
    Replies:
    4
    Views:
    261
    Robert Klemme
    Jun 11, 2007
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    598
    David A. Black
    Jul 2, 2008
Loading...

Share This Page