The easiest way to do what you want in Perl is to use grep{}. Think grep whenever the problem is to filter something - i.e. select a subset of data from a larger set or replace a data set with a subset of itself.

Perl grep is way more powerful than command line grep! Instead of a for loop and some if logic, we have just a single statement as shown below.

@success_array is an array of hash references. Each hash reference enters the grep. If whatever code that is within the grep evaluates to "true", then the hash reference is passed to the output (goes to the left). The code within the grep{} can be any valid Perl statements/conditions that you want. Here it is just a single statement that represents an "if". Note that it is completely legal to assign an array back to itself.

Besides the obvious benefit of being shorter, another benefit is that this avoids all this messing around with subscripts! "off by one" array errors are among the most common problems in programming and many Perl iterators, idioms and techniques are designed to eliminate the need for subscripts. As you write more code the seemingly ubiquitous index "i" in other languages will appear less and less often in your Perl code.

Sometimes there are performance reasons to do something more complicated, but usually the best way is to go with the idiomatic solution.

#!/usr/bin/perl -w use strict; use Data::Dumper; my @success_array = ( { hour=>22, maxDDTF=>2, ydayopen=> -53 }, { hour=>14, maxDDTF=>6, ydayopen=> -71 }, { hour=>03, maxDDTF=>1, ydayopen=> -51}, ); # removes hash references where hash's key of 'ydayopen' is <= -60 + @success_array = grep { $_->{ydayopen} > -60 } @success_array; print Dumper \@success_array; __END__ $VAR1 = [ { 'hour' => 22, 'maxDDTF' => 2, 'ydayopen' => -53 }, { 'hour' => 3, 'maxDDTF' => 1, 'ydayopen' => -51 } ];

In reply to Re: removing a hash from an array of hashes by Marshall
in thread removing a hash from an array of hashes by Conal

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.