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

Hello,

I have this code, but it doesn't work for some reason.

I basically want to loop through the data structure in $results, and remove duplicates by putting the results in a hash, then loop through that hash before entering to the database

foreach my $host_results (@$results) { foreach my $result_tab (@$host_results) { my $uniquekey = $result_tab->{'HostName'}.$result_tab->{'Qid'}; if ($relayqid_hash{$uniquekey}) { # if the relayqid exists ignore my $existing_Recipient = $relayqid_hash{$uniquekey}->{'Recipient +'}; } else { # create the hash record with the result_tab in it $relayqid_hash{$uniquekey}= $result_tab; } } foreach my $host_key (keys %relayqid_hash) { foreach my $result_tab (@$host_key) { my $subject = ($result_tab->{'Subject'}) ? $result_tab->{'Subject' +} : "[no subject]"; # do other stuff not important insert_hash($result_tab); } }

What am I doing wrong here? Any help would be great, I think i'm just looping through and referencing things wrong in the second loop.

Thanks,

Tom

Replies are listed 'Best First'.
Re: References, hashes and arrays
by Abigail-II (Bishop) on May 27, 2004 at 09:36 UTC
    What am I doing wrong here?
    Well, you gave us code, but you didn't tell us what you expect the code to do, and what is happening. And we can't run the code because we have no idea what is in $results. The choice of variable names in the second set of loops suggest something fishy is going on, but whether that is or isn't depends on the structure of $results, and what your intentions are.

    One thing though, a block which only consists of a single assignment to a variable that's lexical to that block is not very useful - unless your RHS has some fetch magic.

    Abigail

Re: References, hashes and arrays
by Anonymous Monk on May 27, 2004 at 10:04 UTC

    Yeah sorry about the vague nature of that question, I guess I was really just trying to work out how to loop through the %relayqid_hash to get at the values in the $result_tab that I put on it in the first loop (if that makes sense - it does to me in my own head! :-)

    Ok, I got it figured anyway, here is what I wanted to do.

    # the top loop as before, then the bottom loop looks like this foreach my $message_key (keys %relayqid_hash) { my $result_tab = $relayqid_hash{$message_key}; my $subject = ($result_tab->{'Subject'}) ? $result_tab->{'Subject'} +: "[no subject]"; insert_hash($result_tab); }

    The bit I was missing was getting at the value of $result_tab in the second loop.

    I hope that makes sense, and if it doesn't... well I guess you probably don't care anyway! :-)

    Cheers,

    Tom

      You could replace your trinary assignment with an or:
      my $subject = $result_tab->{'Subject'} || '[no subject]';
      but it still isn't doing anything. It's getting set, being ignored, and then going out of scope. Do you really want
      $result_tab->{'Subject'} ||= "[no subject]";
      ?

      The PerlMonk tr/// Advocate