in reply to •Re: One liner to Check and undef hash elements
in thread One liner to Check and undef hash elements

Doh, I knew there was a single step approach for Nr.2.. it just wouldn't occur to me last night.

I'm not sure I like your approach to the first one though - using a subref to get around the lack of control in naked blocks seems to hackish. I'm not really happy with my own alternative, either:

my $hash_has_non_undef = do { local $_; my $found = 0; defined && ($found++, last) while (undef, $_) = each %hash; $found; };
But without Perl 6 junctions there's not much that can be done about it if you need shortcircuiting..

Makeshifts last the longest.

  • Comment on Re^2: One liner to Check and undef hash elements (shortcircuiting)
  • Download Code

Replies are listed 'Best First'.
•Re: Re^2: One liner to Check and undef hash elements (shortcircuiting)
by merlyn (Sage) on Apr 16, 2003 at 11:46 UTC
    Well, the non-sub way could look like this:
    my $hash_has_non_undef = do { my $found; until($found or not (undef, m +y $v) = each %hash) { $found = defined $v } $found };
    I mean, that's the way we had to do it in Pascal. Stupid boolean flag variables all over the place because they wouldn't let us exit blocks. {grin}

    Doh! As I'm staring at this, I realize the $v can do double duty.

    my $hash_has_non_undef = do { my $v; until($v or not (undef, $v) = eac +h %hash) { $v = defined $v } $v };
    But that's optimized for Golf, not for maintenance. Ick.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Yeah, I know.. you can at least drop a last in there and simplify the loop condition, if you use a while. Or how about a variant on until:
      my $hash_has_undef = do { local $_; 1 until defined or not (undef, $_) = each %hash; defined; };
      Traded the flag variable for a double defined test on the last value fetched from the hash.

      Makeshifts last the longest.