in reply to Better code for normalizing a list for a HoA?

Why not just use autovivification? Replace all that code with:
push @{$states{$data[0]}}, $data[1];
And let Perl handle the initial empty arrayref and subsequent lookup? What do you think this is, Java? {grin}

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re: •Re: Better code for normalizing a list for a HoA?
by bradcathey (Prior) on Jan 17, 2004 at 21:30 UTC
    Thanks merlyn, but I'm a little embarrassed. I just got P.O.R.&M (the Alpaca book) and was only on Chap 3. Autovivification is in Chap 4 {grin}.

    Anyway for anyone actually following this thread $data[0] and $data[1]need to be switched in meryln's clever one-liner.

    I tried the code and it worked first time, but it does not delete duplicate cities, only duplicate states. So, in my example, 'Dallas' would show up 5 times under 'Texas'.

    Well, on to Chap 4 of the Alpaca book to see how this can be remedied.

    —Brad
    "A little yeast leavens the whole dough."
      Oh, then you want nested hashes for automatic set reduction:
      $states{$data[1]}{$data[0]} = 1; ... for my $state (sort keys %states) { print "$state: ", join(", ", sort keys %{$states{$state}}), "\n"; }
      Yes, read the book faster!

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

        Thanks again merlyn. Extremely tidy code, enough so that I will need to study it to know exactly how it does its "magic." Oh yeah, and read faster.

        —Brad
        "A little yeast leavens the whole dough."