in reply to Difference Quantity of two Arrays

The 1st pass through map allows "user" and "smtp". ("fetch" would be skipped.)
The 2nd pass through map allows "user" and "smtp". ("forward" would be skipped.)
The 3rd pass through map allows "smtp". ("user" is skipped.)
The 4th pass through map allows "user". ("smtp" is skipped.)

One solution (Set arithmetic using a hash):

sub create_list { my %dbs = map { $_ => 1 } qw/fetch forward user smtp/; delete @dbs{@_}; return keys %dbs; }

Another solution (Set arithmetic using a superposition):

use Quantum::Superpositions; sub create_list { my $dbs = any qw/fetch forward user smtp/; my $skip = all @_; return eigenstates( $dbs ne $skip ); }

Another solution (Set arithmetic using sets [seemed appropriate at the time]):

use Set::Scalar qw( ); sub create_list { my $dbs = Set::Scalar->new( qw/fetch forward user smtp/ ); my $skip = Set::Scalar->new( @_ ); return ($dbs - $skip)->members(); }

Update: Added second and third snippet.

Replies are listed 'Best First'.
Re^2: Difference Quantity of two Arrays
by ocs (Monk) on Jan 02, 2007 at 15:30 UTC

    Thank you for your explanation.

    My Perl book says, that delete deletes a key value pair. But it doesn't tell me why I have to use @dbs{@_} instead of $dbs{@_} (which apparently does nothing) or %dbs{@_} (which throws a failure) to get the thing working. Can you explain? :)

      $ means you're working with one element.
      @ means you're working with multiple elements.

      @hash{@list}
      is a hash slice. It amounts to
      map { $hash{$_} } @list
      but can be used as an lvalue.

      $hash{@list}
      is the same as
      $hash{join($;, @list}}
      It's a means of doing Hash of Hashes without actually using Hash of Hashes. It's generally unused.