in reply to Returning 1 entry from a conditional grep call to a hash value

If you're sure to be the only iterator on %links at one time, you can use an iteration walk:
while (my ($key, $value) = each %links) { if (defined $value->{visited}) { ... do something with $links{$key} ...; ) }

-- Randal L. Schwartz, Perl hacker

  • Comment on •Re: Returning 1 entry from a conditional grep call to a hash value
  • Download Code

Replies are listed 'Best First'.
Re: •Re: Returning 1 entry from a conditional grep call to a hash value
by S_Shrum (Pilgrim) on Mar 29, 2002 at 01:18 UTC

    Just the pessimist in me thinking:

    Will the WHILE see the additional keys that get added to %links within the IF processing block or will it only see the keys that were defined before entering WHILE?

    TIA

    ======================
    Sean Shrum
    http://www.shrum.net

      Oh! If you'll be adding to the keylist, you should use a different approach:
      while (1) { my @todo = grep ! defined $links{$_}{something}, keys %links or last +; while (@todo) { my $one = shift @todo; # process $links{$one} } }
      This presumes that something eventually defines all the "something" keys. If not, you need to use a sweeping strategy instead: I've done that for one of my link checker columns.

      -- Randal L. Schwartz, Perl hacker