This:

foreach my $k1 (keys %$h1) { my @fields = split /,/, $h1->{$k1}; ... }

... will be faster as ...

while (my ($k1, $v1) = each %$h1) { my @fields = split /,/, $v1; ... }

each fetches a key-value pair in one go, and except in the case of some tied hashes, will generally be faster than fetching a key and then looking up the value. Both of your hash loops could be optimised this way.

I have a vague feeling that your two loops could be separated out. Hmmm... I'll have a think about it...

Update... this works...

use 5.010; use strict; sub read_hash { my $data = shift; open my $fh, '<', \$data; my %hash; while (<$fh>) { chomp; my ($key,$value)=split /,/, $_, 2; $hash{$key}=$value; } return \%hash; } my $h1 = read_hash(<<'DATA'); k_a1,val_a1,val_a2,val_a3 k_a2,val_a4,val_a5 DATA my $h2 = read_hash(<<'DATA'); k_b1,val_a1 k_b2,val_a2 k_b3,val_a3 k_b4,val_a4 k_b5,val_a5 DATA my %tmp; while (my ($k, $v) = each $h1) { push @{ $tmp{$_} }, $k for split /,/, $v } while (my ($k, $v) = each $h2) { push @{ $tmp{$_} }, $k for split /,/, $v } for (values %tmp) { next if @$_ < 2; say join ',', @$_ }

If you want the lines output in the same order as your original example, then replace the last two lines with:

say $_ for sort map { join ',', @$_ } grep { @$_ > 1 } values %tmp;
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

In reply to Re: how to loop through hash tables more efficiently by tobyink
in thread updated_again: how to loop through hash tables more efficiently by lrl1997

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.