in reply to •Re: Better code for normalizing a list for a HoA?
in thread Better code for normalizing a list for a HoA?

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."

Replies are listed 'Best First'.
•Re: Re: •Re: Better code for normalizing a list for a HoA?
by merlyn (Sage) on Jan 17, 2004 at 21:33 UTC
    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."