Perl: match two arrays

Discussion in 'Programming' started by rollo.larsen, Jul 25, 2006.

  1. #1
    Hi,

    Im stuck with a little problem.

    I got two arrays

    @notwanted = "james,bob,inga"

    @users = "dick,lisa,bob,elvis"

    I would like to match @users agains @notwanted and save alla users from the that *didn't* match anyone in the @notwanted array and save the result in a new array.

    Anyone got a fresh idea? =)
     
    rollo.larsen, Jul 25, 2006 IP
  2. Darrin

    Darrin Peon

    Messages:
    123
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Nothing fresh. My first thought would be to use a nested loop. Loop through the larger array, and in each iteration loop throuh the smaller looking for a match.

    Depending on the type of array and if it isn't more overhead than it's worth, remove the matched items from your arrays as you go through so you speed up as you go. If the arrays are relatively small I wouldn't worry about removing.
     
    Darrin, Jul 25, 2006 IP
  3. AceT

    AceT Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    What probably would work faster is to put one array in a hash and the loop the other arry for a match
     
    AceT, Jul 27, 2006 IP