Re: HoA question





John W. Krahn wrote:
M. Lewis wrote:
I'm still experimenting with the HoA from a couple of days ago. I just
realized there is the possibility for having duplicate elements of the
array. How do I prevent that?

Use a hash instead of an array.


Thank you John. I wondered about that as I was composing the question.

More to study and learn!

Thanks,
Mike


#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper::Simple;

my %HoA;

while (my $line = <DATA>){
chomp $line;
my ($prod, $flavor) = split ' ', $line, 2;
push @{ $HoA{ $prod } }, $flavor;
}

for my $i ( keys %HoA ) {
print "$i -- @{ $HoA{$i} }\n";
}


my %HoH;

while (my $line = <DATA>){
chomp $line;
my ($prod, $flavor) = split ' ', $line, 2;
$HoH{ $prod }{ $flavor } = ();
}

for my $i ( keys %HoH ) {
print "$i -- @{[ keys %{$HoH{$i}} ]}\n";
}




John

--

The world will end in 5 minutes. Please log out.
20:55:02 up 14 days, 17:46, 0 users, load average: 0.67, 0.40, 0.34

Linux Registered User #241685 http://counter.li.org
.



Relevant Pages

  • Re: HoA question
    ... I'm still experimenting with the HoA from a couple of days ago. ... realized there is the possibility for having duplicate elements of the ... Use a hash instead of an array. ... chomp $line; ...
    (perl.beginners)
  • Re: IF with nested AND
    ... Worked just fine for me with array entered. ... John C ... "Joco" wrote: ... which only works for the first cell in the ...
    (microsoft.public.excel.misc)
  • Re: my doesnt totally isolate
    ... John> Hmmm.... ... So how am I supposed to distinguish between an array ... what control you see is an undocumented side effect and may go ... allocation and undef. ...
    (comp.lang.perl.misc)
  • Re: script to find most common last names in a file
    ... John W. Krahn wrote: ... > splitreturns a list in list context so you need list context on the ... > splitwill store its results in the @_ array. ... >> # then creat a hash table of first and last names, ...
    (perl.beginners)
  • Re: Second minimum
    ... John D'Errico wrote: ... > Ind is the index into the row of the sorted array which ... > Now use the tags array to back out where it came from ... > preferably the same, cat. ...
    (comp.soft-sys.matlab)