Re: HoA question



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.

#!/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
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
.



Relevant Pages

  • Re: HoA question
    ... John W. Krahn wrote: ... realized there is the possibility for having duplicate elements of the ... array. ... chomp $line; ...
    (perl.beginners)
  • Re: hash of arrays
    ... You have an array reference in $HOA{$key}, ... documentation of shift and about array slices in ... perldata (`perldoc perldata`). ...
    (perl.beginners)
  • Re: cgi Calendar continued.
    ... HoH or a HoA so that when I print this data in an HTML ... each time you process an item from the first array. ...
    (perl.beginners)
  • Howto Dynamically Combine Multiple Array in HoA
    ... The number of hash maybe varying from HoA to HoA. ... Size of array is fixed for each HoA. ... My question is how can I construct a dynamic way to combine (append) ...
    (perl.beginners)
  • Re: concatenating multiple matches?
    ... Your code adds only one element from your array. ... HoA is a terrible name for a variable. ... Thanks for your suggestion. ... My goal is not to append the entire array, ...
    (comp.lang.perl.misc)