MisterEd
MisterEd

Reputation: 1735

Access nested hash in Perl HoH without using keys()?

Consider the following HoH:

$h = {
    a => {
           1 => x
    },
    b => {
           2 => y
    },
    ...
}

Is there a way to check whether a hash key exists on the second nested level without calling keys(%$h)? For example, I want to say something like:

if ( exists($h->{*}->{1}) ) { ...

(I realize you can't use * as a hash key wildcard, but you get the idea...)

I'm trying to avoid using keys() because it will reset the hash iterator and I am iterating over $h in a loop using:

while ( (my ($key, $value) = each %$h) ) {
    ...
}

The closest language construct I could find is the smart match operator (~~) mentioned here (and no mention in the perlref perldoc), but even if ~~ was available in the version of Perl I'm constrained to using (5.8.4), from what I can tell it wouldn't work in this case.

If it can't be done I suppose I'll copy the keys into an array or hash before entering my while loop (which is how I started), but I was hoping to avoid the overhead.

Upvotes: 3

Views: 888

Answers (5)

ikegami
ikegami

Reputation: 386541

No. each uses the hash's iterator, and you cannot iterate over a hash without using its iterator, not even in the C API. (That means smart match wouldn't help anyway.)

Since each hash has its own iterator, you must be calling keys on the same hash that you are already iterating over using each to run into this problem. Since you have no problem calling keys on that hash, could you just simply use keys instead of each? Or maybe call keys once, store the result, then iterate over the stored keys?

Upvotes: 2

DVK
DVK

Reputation: 129509

Why not do this in Sybase itself instead of Perl?

You are trying to do a set operation which is what Sybase is built to do in the first place.

Assuming you retrieved the data from table with columns "key1", "key2", "valye" as "select *", simply do:

-- Make sure mytable has index on key1
SELECT key1
FRIN mytable t1
WHERE NOT EXISTS (
    SELECT 1 FROM mytable t2
    WHERE t1.key1=t2.key1
    AND t2.key2 = 1
)

-----------
-- OR
-----------

SELECT DISTINCT key1
INTO   #t
FROM   mytable

CREATE INDEX idx1_t on #t (key1)

DELETE #t
FROM   mytable
WHERE #t.key1=mytable.key1
AND   mytable.key2 = 1

SELECT key1 from #t    

Either query returns a list of 1st level keys that don't have key2 of 1

Upvotes: -1

Borodin
Borodin

Reputation: 126742

You will almost certainly find that the 'overhead' of aggregating the second-level hashes is less than that of any other solution. A simple hash lookup is far faster than iterating over the entire data structure every time you want to make the check.

Upvotes: 1

ryansstack
ryansstack

Reputation: 1476

are you trying to do this without any while loop? You can test for existence in a hash just by referencing it, without generating an error

while (  my ($key, $value) = each %{$h} ) {
    if ($value->{1}) { .. } 

}

Upvotes: 0

cjm
cjm

Reputation: 62109

Not really. If you need to do that, I think I'd create a merged hash listing all the second level keys (before starting your main loop):

my $h = {
    a => {
           1 => 'x'
    },
    b => {
           2 => 'y'
    },
};

my %all = map { %$_ } values %$h;

Then your exists($h->{*}->{1}) becomes exists($all{1}). Of course, this won't work if you're modifying the second-level hashes inside the loop (unless you update %all appropriately). The code also assumes that all values in $h are hashrefs, but that would be easy to fix if necessary.

Upvotes: 3

Related Questions