Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

map question

by catfish1116 (Beadle)
on Jul 26, 2019 at 16:48 UTC ( [id://11103468]=perlquestion: print w/replies, xml ) Need Help??

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

I have a small program that is outputs the exact opposite of what I would expect

my @required = qw(preserver sunscreen water_bottle jacket); my %skipper = map { $_, 1} qw(blue_shirt hat jacket preserver sunscreen); foreach my $item (@required) { unless ( $skipper {item} ) { # not found in list? print "Skipper is missing $item, \n"; } }

This is my output Skipper is missing preserver, Skipper is missing sunscreen, Skipper is missing water_bottle, Skipper is missing jacket, How could that be flagged as missing if it is included in hash? TIA, The Catfish

Replies are listed 'Best First'.
Re: map question
by toolic (Bishop) on Jul 26, 2019 at 16:54 UTC
    You should have used the variable $item as the key instead of the string item. Change:
    unless ( $skipper {item} ) { # not found in list?
    to:
    unless ( $skipper {$item} ) { # not found in list?
Re: map question (hash slice)
by LanX (Saint) on Jul 26, 2019 at 19:25 UTC
    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

      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

      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

Re: map question
by stevieb (Canon) on Jul 26, 2019 at 16:57 UTC

    This code reminds me a lot of the exercises in the "Learning Perl Objects, References and Modules" book :)

    That book was instrumental of me finally really grasping references in Perl so many years ago.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-23 15:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found