Sayed, Irfan (Irfan) am Mittwoch, 29. November 2006 08:34:

> I have two arrays . one array contains the element which needs to be
> deleted from the secound array in which that element is already present.
>
> I have tried using delete function but somehow i did not succeed.

Did you check how "somehow" it failed? You could have used Data::Dumper to see 
the effect of delete $array[$i]. See also

perldoc -f delete

Here's one way to do it. remove() takes references to the array for speed. 

The sub could be adapted, f.ex to return a modified copy of $targ instead of 
changing it directly, and/or return the number of actually deleted elements, 
and/or the elements that could not be deleted. 

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

my @wanted=qw / a   c   /; # delete this...
my @target=qw / a b c d /; # ...from this

# deletes contents of second array from first
# $targ is directly changed. Preserves order.
#
sub remove {
  my ($targ, $want)[EMAIL PROTECTED];

  ref($targ) eq 'ARRAY' and ref($want) eq 'ARRAY' 
    or die 'wrong usage'; # check arguments
  
  my %tmp=map {$_=>1} @$want; # to make lookup faster

  @$targ=grep { !exists $tmp{$_} } @$targ;
}

remove ([EMAIL PROTECTED], [EMAIL PROTECTED]);

print @target, "\n";

__END_

There may be a more direct way.

Dani

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to