E
ela
For a market basket analysis, I'd like to traverse a hash structure
containing entries like:
my %list;
$list{"c1"} = "milk%cheese%coca cola";
$list{"c2"} = "coca cola";
$list{"c3"} = "beef";
....
$list{"cn-1"} = "apple%cheese";
$list{"cn"} = "beef%milk";
so the knowledge derived may look like:
c1 is associated with c2, .., cn-1, cn
c3 is associated with cn
....
i.e. the total number of checking is "n + n-1 + ... + 2 + 1" .
While this can be easily achieved if the structure is array, the following
looks clumsy... any better implementation?
while (%list) {
$assign = 1;
foreach $key (keys %list) {
if ($assign == 1) {
$head = $key;
$assign = 0;
next;
}
if ($list{$head} =~ /$list{$key}/ or $list{$key} =~
/$list{$head}/ ) {
print "$list{$head}\t$list{$key}\t1\n";
}
}
undef $list{$head};
}
containing entries like:
my %list;
$list{"c1"} = "milk%cheese%coca cola";
$list{"c2"} = "coca cola";
$list{"c3"} = "beef";
....
$list{"cn-1"} = "apple%cheese";
$list{"cn"} = "beef%milk";
so the knowledge derived may look like:
c1 is associated with c2, .., cn-1, cn
c3 is associated with cn
....
i.e. the total number of checking is "n + n-1 + ... + 2 + 1" .
While this can be easily achieved if the structure is array, the following
looks clumsy... any better implementation?
while (%list) {
$assign = 1;
foreach $key (keys %list) {
if ($assign == 1) {
$head = $key;
$assign = 0;
next;
}
if ($list{$head} =~ /$list{$key}/ or $list{$key} =~
/$list{$head}/ ) {
print "$list{$head}\t$list{$key}\t1\n";
}
}
undef $list{$head};
}