traversing a hash structure


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};
}
 
Ad

Advertisements

U

Uri Guttman

HL> Just by the way, is this real code? Surely Perl would have tried to
HL> parse %cheese etc as the names of hash variables? Confusing at best.

confusing maybe but hashes don't interpolate in strings.

uri
 
S

sln

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};
}

Its still being treated as a list in the foreach loop.
In your current implementation, the %list keys never
change so you sit in the while() loop forever, and
$head is assigned the same $key every time, doing the
same operation indefinitely and generating uninitialized
value warnings in the regex.

change this:
undef $list{$head};

to this:
delete $list{$head};

As a final note, the target of a regular expression
doesen't transparently translate into a regular expression itself.

-sln
 
Ad

Advertisements

E

ela

Its still being treated as a list in the foreach loop.
In your current implementation, the %list keys never
change so you sit in the while() loop forever, and
$head is assigned the same $key every time, doing the
same operation indefinitely and generating uninitialized
value warnings in the regex.

change this:

to this:
delete $list{$head};

As a final note, the target of a regular expression
doesen't transparently translate into a regular expression itself.

-sln

Thanks sln. I hope you have already got your favorite job. Thanks again :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top