Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

deleting elements of one array from another

by jonnyfolk (Vicar)
on Nov 22, 2002 at 23:58 UTC ( [id://215300]=perlquestion: print w/replies, xml ) Need Help??

jonnyfolk has asked for the wisdom of the Perl Monks concerning the following question:

I have an input array @in and and array form a database @db. I need to run each of the scalars in @in against @db and delete from @db each time the scalar is matched. for one scalar $db I would use (I think):
foreach $in (@in) { $db =~ s/$in/g; }
but trying to run one array against the other has me stumped (rather like the English cricket team on the ashes tour at the moment! :-))

Replies are listed 'Best First'.
Re: deleting elements of one array from another
by jmcnamara (Monsignor) on Nov 23, 2002 at 00:13 UTC

    One way to do it is to store the array values in a hash and remove the existing element from @db using grep:
    #!/usr/bin/perl -wl use strict; my @db = qw(aaa bbb ccc ddd eee fff); my @in = qw(aaa fff); my %h; # Initialise the hash using a slice @h{@in} = undef; print "@db"; @db = grep {not exists $h{$_}} @db; print "@db"; __END__ Prints: aaa bbb ccc ddd eee fff bbb ccc ddd eee

    --
    John.

Re: deleting elements of one array from another
by Orsmo (Beadle) on Nov 23, 2002 at 04:14 UTC

    Use a hash slice to build a lookup table:

    use strict; use warnings; my @db = (1, 3, 5, 7, 9, 11); my @in = (1, 2, 5, 8, 9, 10, 13); my %lookup; my @result; @lookup{@in} = (); foreach my $elem (@db) { push(@result, $elem) unless exists $lookup{$elem}; } print "@result\n"; # should print 3 7 11.

    Of course, this doesn't actually remove the elements from @db. It creates a new array, @result, that holds only those elements of @db that were not also on @in. If your array is very, very large and you wish to avoid making a copy, this could be a problem.

(bbfu) Re: deleting elements of one array from another
by bbfu (Curate) on Nov 23, 2002 at 04:24 UTC

    From your example code (ignoring the [assumed typo] syntax error), it seems like you don't necessarily want to remove the array element but, rather, want to delete any occurrences of $in in each element of @db. If that's the case, just use a nested loop...

    foreach my $db (@db) { foreach my $in (@in) { $db =~ s/$in//g; } }

    But if you actually want to remove any elements of @db that exactly match one of the elements of @in, then use one of the previous suggestions. :)

    bbfu
    Black flowers blossum
    Fearless on my breath

Re: deleting elements of one array from another
by jonnyfolk (Vicar) on Nov 23, 2002 at 05:54 UTC
    Thanks very much for your help, one and all. I have not yet used hashes in anger - although I have read much about them.

    Now is my chance to try them out. Cheers!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://215300]
Approved by Tanalis
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-26 06:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found