in reply to map question

Toolic already gave you the right answer.

But you might be interested in another approach using hash-slices, which is way faster for big data.

use strict; use warnings; my @got = qw(blue_shirt hat jacket preserver ); # sunscreen); my @required = qw(preserver sunscreen water_bottle jacket); # ---------- directly coded my %required; @required{@required}=@required; delete @required{@got}; warn "Skipper is missing: ", values %required; # ---------- or alternative as custom function warn "Skipper is missing: ", array_minus(@got,@required); sub array_minus (\@\@) { my ( $got, $required ) = @_; my %required; @required{@required}=@required; delete @required{@got}; return values %required; }

Skipper is missing: water_bottle sunscreen at d:/exp/hash_diff.pl l +ine 14. Skipper is missing: sunscreen water_bottle at d:/exp/hash_diff.pl l +ine 28.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Replies are listed 'Best First'.
Re^2: map question (hash slice)
by AnomalousMonk (Archbishop) on Jul 29, 2019 at 13:38 UTC
    sub array_minus (\@\@) { my ( $got, $required ) = @_; my %required; @required{@required}=@required; delete @required{@got}; return values %required; }

    Note also that this function is not correct because the  $got $required array references passed as arguments are never used. As it stands, the code here works only because  @required @got within the function access file-scope lexicals defined earlier in the code. Also, the prototype  array_minus(\@\@) needs to be declared (or the function itself needs to be defined) before the function is invoked for prototyping to be effective.

    c:\@Work\Perl\monks>perl use strict; use warnings; sub array_minus (\@\@); my @got = qw(blue_shirt hat jacket preserver ); my @required = qw(preserver sunscreen water_bottle jacket); # ---------- as custom function warn "Skipper is missing: ", array_minus(@got,@required); sub array_minus (\@\@) { my ( $ar_got, $ar_required ) = @_; my %required; @required{@$ar_required} = @$ar_required; delete @required{@$ar_got}; return values %required; } __END__ Skipper is missing: water_bottlesunscreen at - line 11.


    Give a man a fish:  <%-{-{-{-<

      True, my bad!

      Thanks for correcting.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re^2: map question (hash slice)
by ikegami (Patriarch) on Jul 29, 2019 at 03:19 UTC

    If you're going for speed, then don't make a needless copy of everything in @required.

    my %required; @required{@required}=(); delete @required{@got}; warn "Skipper is missing: ", keys %required;

    Of course, this assumes string values.

      > Of course, this assumes string values.

      Not assuming it is why I went this way. It can also handle an array of refs like this.

      Though mixed values may clash.

      I started a thread here once where I asked for a solution for this, will dig it out tomorrow.

      update

      couldn't wait Re: Using hashes for set operations... , but doesn't really help.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        Except your code assumes strings too. For "objects", you'd need

        my %required; @required{ map key($_), @required } = @required; delete @required{ map key($_), @got }; warn "Skipper is missing: ", values %required;

        Where key is a suitable key-producing sub.