in reply to Re: The Cost of Nested Referencing
in thread The Cost of Nested Referencing

on linear code like that it may make things hader to read, but I don't see what's wrong with

my %hash = ( ..... ); foreach my $key1 (@keyset1) { my $inner = $hash{$key1}; foreach my $key2 (@keyset2) { print $inner->{$key2}; } }

instead of

my %hash = ( ..... ); foreach my $key1 (@keyset1) { foreach my $key2 (@keyset2) { print $hash{$key1}{$key2}; } }

That doesn't sacrifice too much in readability, I think

Replies are listed 'Best First'.
Re^3: The Cost of Nested Referencing
by Aristotle (Chancellor) on Nov 20, 2002 at 13:54 UTC
    They're both far too verbose.
    for my $subhash (@hash{@keyset1}) { print $subhash->{$_} for @keyset2; }
    or if you really only have a single statement in there, even print @{$_}{@keyset2} for @hash{@keyset1}; You can be efficient and readable all at the same time.

    Makeshifts last the longest.

      I *personally* would rather stay away from

      statement if condition statement for(@list)

      ...because that way I'm less likely to be bashed by others claiming that (my) perl code is unreadable ;)

        Why? What's more readable about
        foreach my $foo (@bar) { do_something($foo); }
        than do_something($_) foreach @bar; ? All I see is twice as much to type and read with absolutely no difference to the clarity. Especially when you nest a few of those and it ends up something like
        for $blah (@blah) { # ... # ... # ... if($whatever) { last; } while($foo) { if(@bar) { # ... while($baz) { # ... # ... # ... } if($whatever) { next; } elsif($snafu) { last; } else { # ... } # ... # ... } # ... } } # ... # ... }
        It's atrocious. Please use a few last if ... and spare me the effort of having to eyeparse 15 levels of indentation.

        Makeshifts last the longest.

Re: The Cost of Nested Referencing
by Abigail-II (Bishop) on Nov 19, 2002 at 10:11 UTC
    Beauty is in the eye of the beholder, but I find the latter far more readable that the former.

    Abigail