Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

comparing two array's in perl

Status
Not open for further replies.

bcdixit

Technical User
Nov 11, 2005
64
US
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
 
This works:

#! /usr/bin/perl
use strict;
use warnings;


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


foreach my $i ( 0..$#exclude ) {

foreach my $x ( 0..$#array ) {

if ( $array[$x]
&& $exclude[$i]
&& $array[$x] eq $exclude[$i] ) {
splice( @array, $x, 1 );
#delete( $array[$x] );
#next;
}
}
}

print join (' ', @array) ."\n";
 
this will do exactly wha you said you want done, remove the elements from the array but not shorten the array:
Code:
[blue]@array[/blue] = [red]([/red][red]"[/red][purple]john[/purple][red]"[/red],[red]"[/red][purple]larry[/purple][red]"[/red],[red]"[/red][purple]kevin[/purple][red]"[/red],[red]"[/red][purple]peter[/purple][red]"[/red],[red]"[/red][purple]mike[/purple][red]"[/red][red])[/red][red];[/red]
[blue]@exclude[/blue] = [red]([/red][red]"[/red][purple]john[/purple][red]"[/red],[red]"[/red][purple]kevin[/purple][red]"[/red][red])[/red][red];[/red]

[olive][b]for[/b][/olive] [url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$i[/blue] [red]([/red][blue]@exclude[/blue][red])[/red] [red]{[/red]
   [olive][b]for[/b][/olive] [black][b]my[/b][/black] [blue]$n[/blue] [red]([/red][fuchsia]0[/fuchsia] .. [blue]$#array[/blue][red])[/red] [red]{[/red]
      [olive][b]if[/b][/olive] [red]([/red][blue]$array[/blue][red][[/red][blue]$n[/blue][red]][/red] eq [blue]$i[/blue][red])[/red] [red]{[/red]
         [url=http://perldoc.perl.org/functions/delete.html][black][b]delete[/b][/black][/url] [blue]$array[/blue][red][[/red][blue]$n[/blue][red]][/red][red];[/red]
         [olive][b]last[/b][/olive][red];[/red]
      [red]}[/red]
   [red]}[/red]
[red]}[/red]
[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][blue]$_[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red] [olive][b]for[/b][/olive] [blue]@array[/blue][red];[/red]

if there can be duplicates you also want deleted rem or remove "last" from the inner loop. If you really want to shorten the array then this is not the way to do what you want.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
thanks kevinADC.. exactly what i wanted.
 
your original code seems to work for me (after correcting some syntax errors in the posted code), at least with your sample data.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I just found out that in my actual problem, foreach element in the @array it had white space characters till the end. I removed those and looks my original code is now working.
thanks for your help anyway.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top