in reply to Hash of Arrays - Sort over all array entries

#!/usr/bin/perl -l use strict; use warnings; no warnings qw /syntax/; my %HoA = ( simpsons => [qw /homer marge bart/], flintstones => [qw /fred barney/], jetsons => [qw /george jane elroy astro/], ); print for sort map {my $name = $_; map {"$_:$name"} @{$HoA {$name}}} k +eys %HoA;

Of course, whether this is 'easier' is in the eye of the beholder.

Abigail

Replies are listed 'Best First'.
Re: Re: Hash of Arrays - Sort over all array entries
by knexus (Hermit) on Sep 09, 2003 at 22:32 UTC
    Thanks Abigail-II and tye for the info. This method reduces the number of times through loops from 4 7 in my case, to 2(?) 5.

    Of course, whether this is 'easier' is in the eye of the beholder.

    Being new to perl I really have to stare at the method above to get the sense of it (I really haven't used map much). So, indeed 'easier' does depend on what you are looking for.

    Thanks again.

    Update: Changed faulty # of loops :-)

      <bq> This method reduces the number of times through loops from 4 in my case, to 2(?). </bq>

      Actually, I count 5 loops: keys, map, map, sort, for.

      Abigail

      This method reduces the number of times through loops

      The number of loops is usually less important than how they are nested. 5 loops, none of them nested means run-time on the order of O(5*$n) which is O($n). You had three of your loops nested which is more like O($n**3).

      In this case, the loop sizes are more like $n (number of families), $m (number of members per family), and $n*$m (total number of members) so we go from O( ($n*$m)**2 ) to just O($n*$m).

                      - tye