S_Shrum has asked for the wisdom of the Perl Monks concerning the following question:

This is in regards to my question concerning looping on a growing hash. For more background on this refer to How do I...? - Looping on a growing hash.

First...thanx to kanji and jeffa for their help on the LOOP trigger.

Some background
=============================

I have a hash called %links that is structured in the following manner:

%links{URL_NAME}{count}     # of occurances (this URL)
                {depth}     Number of pages from root doc
                {title}     text in TITLE tag
                {text}      HTML-stripped contents of page
                {visited}   flag to indicate traversal

The URL_NAME key will be the absolute URL (http://www.someserver.com/somedir/somepage.html) to the page in question. This %links hash will contain a list of URLs that I want to visit in order to build a data file from the resulting traversal with the data in the completed %links hash.

What I'm attempting to do
=============================

I am looking for the best way to retrieve a entry from the hash that has {visited} UNDEFined (or maybe even set to 0...must likely UNDEFined). The current suggestions are to do something along the lines of:

my @keys = grep ! defined $links{$_}->{'visi ted'}, keys %links;

However, this code recreates the array with every value in the hash that matches the condition which is more than I want. Besides, the hash could contain hundreds of entries and this just adds to the processing overhead.

To keep processing fast and extranous information at a minimum, I only need 1 URL_NAME to be returned at a time. The returning order is unimportant as the loop will process all the entries anyways.

Is it possible to return just 1 entry and reduce total processing cycles? If so, how?

A better question might be: Am I really speeding things up by getting 1 entry....will the REGEX process the entire hash before returning 1 entry anyways? Is processing an issue on an array that contains X number of entries that gets recreated X - 1 times each pass?

As always...pro/con input graciously accepted.

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

Replies are listed 'Best First'.
•Re: Returning 1 entry from a conditional grep call to a hash value
by merlyn (Sage) on Mar 29, 2002 at 01:04 UTC
    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

      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

Re: Returning 1 entry from a conditional grep call to a hash value
by S_Shrum (Pilgrim) on Mar 29, 2002 at 01:33 UTC

    Just to clarify....the problem here is that %links will have additional entries added to it DURING it's processing...this is the very root of the problem.

    Building the hash first AND THEN processing it is just double-duty and will make a script that already takes forever take twice as long.

    TIA

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