Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
... find out (in a loop) which of those are empty?

One way for an array:

c:\@Work\Perl\monks>perl -wMstrict -le "use Data::Dump qw(dd); ;; use constant INITIALIZER => ''; ;; my @strings = (INITIALIZER) x 6; dd \@strings; ;; @strings[1, 4] = qw(foo bar); dd \@strings; ;; my @empties = grep { $strings[$_] eq INITIALIZER } 0 .. $#strings; print qq{indices of empty elements: @empties}; " ["", "", "", "", "", ""] ["", "foo", "", "", "bar", ""] indices of empty elements: 0 2 3 5
And a way for hashes:
c:\@Work\Perl\monks>perl -wMstrict -le "use Data::Dump qw(dd); ;; use constant INITIALIZER => ''; ;; my %strings = map { $_ => INITIALIZER } 0 .. 5; dd \%strings; ;; @strings{1, 4} = qw(foo bar); dd \%strings; ;; my @empties = grep { $strings{$_} eq INITIALIZER } keys %strings; print qq{keys of empty values: @empties}; " { "0" => "", "1" => "", "2" => "", "3" => "", "4" => "", "5" => "" } { "0" => "", "1" => "foo", "2" => "", "3" => "", "4" => "bar", "5" => +"" } keys of empty values: 3 0 2 5
(Keys of empty values are returned in random-ish order because hashes have no inherent ordering other than the key-value pairing of each hash element.)

Update: From Perl version 5.12 onward, keys called in list context also acts with arrays to return a list of all the indices (the "keys", get it?) of the array, so in the array example, the statement
    my @empties = grep { $strings[$_] eq INITIALIZER } keys @strings;
would have worked as well.


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


In reply to Re: Declaring and checking content of variables with consecutive names by AnomalousMonk
in thread Declaring and checking content of variables with consecutive names by rflesch

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (6)
    As of 2019-10-17 22:44 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Notices?