Checkout Filter

I will expect you mean the values of the keys of the hash are arrays, which is a common structure. For selecting, grep is a good operator. You also want to understand how to access the arrays to carry out the conditional test.

To build an array of the keys of arrays held by the keys of baskets which have 10 items or less:

my %baskets = ( ten => [0..9], bat => [0..8,'bat:-:laser-disruptor'], elf => [0..10], ); my @tenorless = grep { @{ $baskets{$_} } =< 10 } keys %baskets; # print @tenorless # batten

In this example, we access the array by providing the hashvalue through the key and dereferencing it with the array sigil and curly braces. Obtaining the scalar context of number of items.

You can now happily process the baskets in your 10 items or less queue, the baskets with more having been laser blasted to smithereens.

Of the baskets holding the requisite number of items, some contain goods other than groceries, such as hero equipment, which need to be checked out in the kickass department. Now we really need to access the items inside the arrays. To do this we pass in our newly formed list to a further grep. Similar to adding further foreach loops.

All we need to watch for is assigning the special variable $_ in each 'outer' grep we use so we can repel magnetic pulse traps and displace gravity gracefully.

#!usr/bin/perl use warnings; use strict; my @batgoods = map {'bat:-:'.$_}qw(night-visor laser-disruptor zipwire +-refill); my %baskets = ( ten => [0..9], bat => [0..8,'bat:-:laser-disruptor'], elf => [0..10], ); my @tenorless = grep { @{$baskets{$_}} <= 10 } keys %baskets; my @hasbat = grep { my $key = $_; grep { my $batitem = $_; grep {$_ =~ $batitem} @{$baskets{$key}}; } @batgoods; } @tenorless; print @hasbat; # bat exit 0;

grep, foreach for heros and heroines!


In reply to Re: question about what to grep(?) by Don Coyote
in thread question about what to grep(?) by crunch_this!

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.