1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Perl - string is in array question

Discussion in 'Programming' started by sarahk, Jun 28, 2005.

  1. #1
    This is dead simple in PHP but I'm doing a small script in perl, and simple perl

    
    #I have 2 arrays with very similar content
    #@pids
    #@outs
    #I need to know which values don't exist in the other array
    
    print "Orphaned Processes\n";
    foreach $pid (@pids){
      #$found = grep/$pid/@outs;
      $found = grep(/$pid/,  @outs);
      if ($found > 0)
      {
         print "proc: $pid\n";
      }
    }
    
    print "Orphaned Out Files\n";
    foreach $thisout (@outs){
      $found = grep(/$thisout/, @pids);
      if ($found > 0)
      {
        print "$thisout\n";
      }
    }
    
    Code (markup):
    The grep doesn't seem to be working. Any ideas?

    thanks

    Sarah
     
    sarahk, Jun 28, 2005 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #2
    sarahk, Jun 28, 2005 IP
  3. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I havn't had to use perl for a *long* time, but you might want to try this: (Not tested but should work)
    
    my $found = 0;
    # Loop thru @pids
    print "Orphaned processes \n";
    foreach $pid(@pids){
      $found = 0;
      # For each $pid, loop thru all @outs
      foreach $out(@outs){
        # Does $out contain $pid ?
        if ($out =~ /$pid/) {
          # $out contains $pid
          $found = 1;
        } else {
          # $out does not contain $pid
        }
      }
      if ($found == 0) {
        print "$pid \n";
      }
    }
    
    Code (markup):
     
    exam, Jul 2, 2005 IP