proper use of hash of hashes?

Discussion in 'Perl Misc' started by Dan Jacobson, Apr 18, 2005.

  1. Dan Jacobson

    Dan Jacobson Guest

    This program does what I want, but is it a proper use of a hash of
    hashes, etc.?
    $ cat data
    5a m g t
    4g k x
    5a t c
    $ perl < data
    4g: k x
    5a: c g m t
    $ cat
    use strict;
    use warnings;
    my %k;
    while (<>) {
    my @F = split;
    my $h = shift @F;
    for (@F) { $k{$h}{$_} = undef }
    for ( keys %k ) { printf "$_: %s\n", join " ", sort keys %{ $k{$_} } }
    Dan Jacobson, Apr 18, 2005
    1. Advertisements

  2. Yes, you're using all your hashes to eliminate duplicates: The outer
    hash to summarize your input lines, and the inner hashes to guarantee
    each character exists therein only once.
    You could do this in just one line.

    my( $h, @F ) = split;
    Less parantheses provides more readability, IMHO.

    $k{$h}{$_} = undef for @F;

    Alternatively, you could doing a hash slice here:

    @{ $k{$h} }{ @F } = ();
    Fabian Pilkowski, Apr 19, 2005
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.