comparing two arrays in perl

Discussion in 'Perl Misc' started by bcdixit, Aug 27, 2007.

  1. bcdixit

    bcdixit Guest

    Hi,
    I have two arrays, for example,

    @array = ("john","larry","kevin","peter","mike")
    @exclude = ("john","kevin")

    i want to delete from @array , values that exist in @exclude.
    i.e. i want my @array to look like

    @array("","larry","","peter","mike")

    i have written this code but it doesnot seem to work.

    foreach$i(0..$#exclude)
    {
    foreach$x(0..$#array)
    {
    if($array[$x] eq $exclude[$i])
    {
    delete($array[$x]);
    next;
    }
    }
    }

    am I missing something? is there someother better way to do it in
    perl?

    thanks
    -bd
     
    bcdixit, Aug 27, 2007
    #1
    1. Advertisements

  2. What you need is an easy way to check if an value is in @exclude. This
    can be done by converting it to a hash where easch element from
    @exclude is an key with an true value:

    This can be done this way

    %exclude = map { $_ => 1 } @exclude;

    or this way:

    $exclude{$_} = 1 for @exclude;

    (and probbaly many other ways).

    Then you just need to iterate over @array one time.

    //Makholm
     
    Peter Makholm, Aug 27, 2007
    #2
    1. Advertisements

  3. bcdixit

    Paul Lalli Guest

    Your Question (or at least a variant of it) is Frequently Asked...
    $ perldoc -q "difference of two arrays"
    Found in /opt/perl/lib/5.6.1/pod/perlfaq4.pod
    How do I compute the difference of two arrays? How do I
    compute the intersection of two arrays?
    Does not work is the worst possible error description. *HOW* does it
    not work? What results does it produce that you did not expect?
    delete(), when used on array elements, will undefine internal
    elements, and will remove the last element. This is generally not a
    good thing, and also doesn't match your desired output. I think you
    instead want:
    $array[$x] = "";
    "next" being the last statement in a block is a no-op. It simply says
    to go up to the beginning of the current loop and start the next
    iteration. That's what would happen anyway. I think you meant "last"
    here.
    You tell me. The code (after being modified as I suggest) does
    exactly what you say you want. If you disagree, post a short-but-
    complete script with desired output and actual output.
    Yes, use hashes, as Peter and the FAQ both suggested.

    Paul Lalli
     
    Paul Lalli, Aug 27, 2007
    #3
  4. Yes. It is very bad to change the array (add or remove elements) while
    looping through it using foreach().
    Like explained in the FAQ: "How do I compute the difference of two arrays?
    How do I compute the intersection of two arrays?"

    jue
     
    Jürgen Exner, Aug 27, 2007
    #4
  5. bcdixit

    Paul Lalli Guest

    The only element that could be deleted from that array is the last
    one. I don't see how that could affect anything, as it would also
    therefore be the last iteration of the loop.

    Paul Lalli
     
    Paul Lalli, Aug 27, 2007
    #5
  6. bcdixit

    jkstill Guest

    This looks promising:

    http://www.perlmonks.org/index.pl?node=429761
     
    jkstill, Aug 28, 2007
    #6
  7. bcdixit

    jkstill Guest

    My bad, replied to the wrong thread.
     
    jkstill, Aug 28, 2007
    #7
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.